首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost mpl折叠占位符表达式无法编译

boost mpl折叠占位符表达式无法编译
EN

Stack Overflow用户
提问于 2015-02-13 10:13:37
回答 1查看 321关注 0票数 2

我正在尝试从boost-mpl编译Statemachine示例(位于libs/mpl/examples/fsm/player2.cpp中),但是boost版本1.37和g++ 4.8.2失败了。使用boost版本1.56和相同的编译器,构建就成功了。不幸的是,由于一些平台限制,我无法切换到1.56版本。

我不希望任何人查看上面提到的冗长的示例,因此我确定了一个说明问题的最小代码片段:

代码语言:javascript
复制
#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;

对于boost 1.37,g++发出以下错误:

代码语言:javascript
复制
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;

是否有办法使代码用boost 1.37编译?我在网上搜索已经有一段时间了。尽管如此,如果这个问题已经在某个地方得到了回答,我希望你能指出这一点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-14 01:20:24

在那个古老版本的boost中,看起来很简单的一个bug。

一个快速的二分法告诉我,它是固定在v1.43.0(平方米)。发行说明不公开这个秘密,但git却公开了:

显然是后者(通过针对31a2c78进行编译来证实)。

因此,您在include/boost/mpl/aux_/template_arity.hpp(立方)中修复了这一行:

代码语言:javascript
复制
sizeof(arity_helper(type_wrapper<F>(),arity_tag<N>())) - 1

应该是

代码语言:javascript
复制
sizeof(::boost::mpl::aux::arity_helper(type_wrapper<F>(),arity_tag<N>())) - 1

当然,解决这个问题的正确方法是使用支持的boost版本。

(2008年11月3日)

(2010年5月6日)

警告:也存在于头的预处理版本中生成的几份副本中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28497165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档