boost mpl fold占位符表达式无法编译
boost mpl fold placeholder expression fails to compile
我正试图从boost mpl(位于libs/mpl/examples/fsm/player2.cpp中)编译Statemachine示例,但它在boost版本1.37和g++4.8.2中失败了。使用boost版本1.56和相同的编译器,构建成功。不幸的是,由于一些平台限制,我无法切换到1.56版本。
我不希望有人研究上面提到的冗长的例子,因此我确定了一个说明问题的最小代码片段:
#include <boost/mpl/fold.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/placeholders.hpp>
namespace mpl = boost::mpl;
using namespace mpl::placeholders;
//Basic queue datatype
template< class CURRENT, class NEXT >
struct queue_element
{
typedef typename CURRENT::mytype mytype;
};
//type to be put at the end of the queue
struct default_queue_element
{
};
template <class TYPE>
struct wrapper{
typedef TYPE mytype;
};
typedef mpl::vector<wrapper<int>, wrapper<char> > myvector;
//the following fold expression should create this type:
typedef queue_element<wrapper<char>, queue_element<wrapper<int>,
default_queue_element> > this_type_should_be_created;
//This typedef fails to compile with boost Version 1.37,
//but works perfectly with version 1.56
typedef typename
mpl::fold<
myvector
,default_queue_element
,queue_element<_2,_1>
>::type
generate_queue;
在助推1.37,g++发布以下错误:
foldtest2.cpp: In instantiation of ‘struct queue_element<mpl_::arg<2>, mpl_::arg<1> >’:
../boost_1_37_0/boost/mpl/aux_/preprocessed/gcc/template_arity.hpp:85:5: required from ‘const int boost::mpl::aux::template_arity_impl<queue_element<mpl_::arg<2>, mpl_::arg<1> >, 1>::value’
../boost_1_37_0/boost/mpl/aux_/preprocessed/gcc/template_arity.hpp:93:5: required from ‘const int boost::mpl::aux::template_arity<queue_element<mpl_::arg<2>, mpl_::arg<1> > >::value’
../boost_1_37_0/boost/mpl/aux_/preprocessed/gcc/template_arity.hpp:98:30: required from ‘struct boost::mpl::aux::template_arity<queue_element<mpl_::arg<2>, mpl_::arg<1> > >’
../boost_1_37_0/boost/mpl/aux_/preprocessed/gcc/apply.hpp:67:8: required from ‘struct boost::mpl::apply2<queue_element<mpl_::arg<2>, mpl_::arg<1> >, default_queue_element, wrapper<int> >’
../boost_1_37_0/boost/mpl/aux_/preprocessed/gcc/fold_impl.hpp:67:85: required from ‘struct boost::mpl::aux::fold_impl<2, boost::mpl::v_iter<boost::mpl::vector<wrapper<int>, wrapper<char> >, 0l>, boost::mpl::v_iter<boost::mpl::vector<wrapper<int>, wrapper<char> >, 2l>, default_queue_element, queue_element<mpl_::arg<2>, mpl_::arg<1> > >’
../boost_1_37_0/boost/mpl/fold.hpp:39:18: required from ‘struct boost::mpl::fold<boost::mpl::vector<wrapper<int>, wrapper<char> >, default_queue_element, queue_element<mpl_::arg<2>, mpl_::arg<1> > >’
foldtest2.cpp:39:6: required from here
foldtest2.cpp:15:38: error: no type named ‘mytype’ in ‘struct mpl_::arg<2>’
typedef typename CURRENT::mytype mytype;
有没有办法用boost1.37编译代码?我在网上搜索已经有一段时间了。尽管如此,如果这个问题已经在某个地方得到了回答,如果你能指出这一点,我将不胜感激。
看起来只是boost的古老版本中的一个bug。
一个快速平分告诉我它是在v143.0(²)中修复的。发布说明没有透露秘密,但git会:
- c5621d9 MPL:票证#1992的合并修复程序boost::MPL::zip_view不支持用作具有::type的元函数
- 31a2c78 MPL:ticket#4061[MPL]gcc-4.5与arity_helper相关的编译问题的合并修复程序
它显然是后者(通过对31a2c78的编译证实)。
因此,您在include/boost/mpl/aux_/template_arialy.hpp(³):中修复了这一行
sizeof(arity_helper(type_wrapper<F>(),arity_tag<N>())) - 1
应该是
sizeof(::boost::mpl::aux::arity_helper(type_wrapper<F>(),arity_tag<N>())) - 1
当然,解决这个问题的正确方法是使用支持的boost版本
cco(2008年11月3日)!!
²(2010年5月6日)
³警告:也存在于标头的预处理版本中生成的多个副本中
相关文章:
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 断言中的Fold表达式在某些计算机上编译,但在其他计算机上不编译
- 即使使用调试编译标志,表达式也是"optimized out"
- 编译 llvm 3.1 时,为什么会出现错误:在">"标记之前预期主表达式
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- 如何在常量计算表达式中获取编译时错误?
- 不是 VS2017 中的编译时常量表达式
- C++编译错误(有符号和无符号整数表达式之间的比较)
- 将编译时定义大小的数组初始化为常量表达式
- 用MASM编译汇编文件时,缺少表达式操作员
- 如何判断表达式是在编译时还是运行时计算的?
- 基于用户表达式在编译时参数化函数
- 是否可以表示不应编译的表达式的静态_assert
- 使用折叠表达式初始化静态 constexpr 类数据成员不编译
- 当使用无效表达式时,概念是否应该无法编译?
- 单元测试:检查是否不应编译某些表达式
- 减少 std::正则表达式编译时间 C++
- 使用提升正则表达式C++进行编译
- 使用表达式模板编译时间数组索引--constexpr