boost mpl fold占位符表达式无法编译

boost mpl fold placeholder expression fails to compile

本文关键字:编译 表达式 占位符 mpl fold boost      更新时间:2023-10-16

我正试图从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日)

³警告:也存在于标头的预处理版本中生成的多个副本中