gpt4 book ai didi

c++ - 生成巨大的 Boost.MPL 类型序列

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:47:46 24 4
gpt4 key购买 nike

考虑以下自动生成 Boost.MPL 类型序列(列表或 vector )的代码。

    #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 个元素的 vector 。然而,令人惊讶的是它可以生成最多包含 247 个元素的列表!

有人知道为什么会这样吗?

最佳答案

根据 the docs , BOOST_MPL_LIMIT_xxx_SIZE指定序列的 variadic 形式的限制(例如 list<> ); numbered 形式(例如 list42<> )没有预定义的上限,除了编译器对模板参数数量的限制。好吧,后一种说法并不完全准确:实际上,在默认的库配置中,使用预先生成的预处理 header 对编号形式施加了限制;见this post关于如何提起它。

跟进:@rhalbersma 您似乎将两个独立的概念捆绑在一起:列表元素的最大数量与 list 的最大数量。的“构造函数”。 BOOST_MPL_LIMIT_LIST_SIZE controls the latter ,而不是前者,两者之间确实没有依赖关系。您上面的代码正在测试前者;最大模板数量是完全不同的野兽。

首先 MPL 序列存在数量限制的原因是该库必须模拟可变参数模板(它是在 C++11 之前编写的),这通常是这样做的 by defaulting unused arguments to some auxiliary type and providing a bunch of specializations to weed out those unused arguments before constructing the actual sequence .这样做的代价是默认参数通常会出现在错误消息中并掩盖其他所有内容,并且大量特化对编译时间有显着影响。 IOW,你必须在某个地方停下来,当时你似乎不太可能经常需要将超过 20 个序列元素传递给序列的“构造函数”(如果你这样做了,总是有编号的形式),因此当前限制。

关于c++ - 生成巨大的 Boost.MPL 类型序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9508138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com