考虑以下自动生成 Boost.MPL 类型序列(列表或向量)的代码。
#include <iostream> // cout
#include <boost/mpl/for_each.hpp> // for_each
#include <boost/mpl/identity.hpp> // identity, make_identity
#include <boost/mpl/int.hpp> // int_
#include <boost/mpl/list.hpp> // list
#include <boost/mpl/next.hpp> // next
#include <boost/mpl/push_front.hpp> // push_front
#include <boost/mpl/vector.hpp> // vector
template<size_t, typename> struct iota_n;
template<typename Value>
struct iota_n<0, Value>
:
boost::mpl::list<> // can change this to boost::mpl::vector<>
{};
template<size_t N, typename Value>
struct iota_n
:
boost::mpl::push_front< typename
iota_n<
N - 1, typename
boost::mpl::next<Value>::type
>::type,
Value
>
{};
// works for N <= 20 and boost::mpl::vector
// works for N <= 247 and boost::mpl::list
typedef iota_n< 247, boost::mpl::int_<0> >::type sequence;
struct print
{
template<typename T>
void operator()(boost::mpl::identity<T>)
{
std::cout << T::value << "\n";
}
};
int main()
{
boost::mpl::for_each<sequence, boost::mpl::make_identity<> >(
print()
);
std::cout << BOOST_MPL_LIMIT_LIST_SIZE << '\n'; // 20 on my system
std::cout << BOOST_MPL_LIMIT_VECTOR_SIZE << '\n'; // 20 on my system
return 0;
}
根据 Boost.MPL 文档,一个boost::mpl::list
序列最多可以有多个BOOST_MPL_LIMIT_LIST_SIZE
元素,类似地,boost::mpl::vector
编译器最多可以包含BOOST_MPL_LIMIT_VECTOR_SIZE
. 在我的系统上,这两个宏都评估为 20。
MSVC++ 2010 和 Boost 1.47.0 确实不能生成超过记录的 20 个元素的向量。然而,令人惊讶的是,它可以生成包含多达 247 个元素的列表!
有谁知道为什么会这样?