【问题标题】:boost mpl fold placeholder expression fails to compileboost mpl 折叠占位符表达式无法编译
【发布时间】:2015-02-13 10:13:37
【问题描述】:

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

我不希望任何人研究上述冗长的示例,因此我确定了一个最小代码 sn-p 来说明问题:

#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++ 出现以下错误:

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 进行编译?我已经在网上搜索了一段时间。如果这个问题已经在某个地方得到了回答,如果你能指出这一点,我将不胜感激。

【问题讨论】:

  • 对自包含减少的赞誉。这很不错。不出所料,这是我看到 5 年前已解决的问题的唯一原因:/

标签: c++ boost fold boost-mpl


【解决方案1】:

看起来只是那个古老(¹)版本的 boost 中的一个错误。

快速二分法告诉我它已在 v1.43.0(²) 中得到修复。 Release notes不要泄露秘密,但 git 会:

显然是后者(通过针对 31a2c78 进行编译确认)。

所以你修复了 include/boost/mpl/aux_/template_arity.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 版本


¹(2008 年 11 月 3 日)!!

²(2010 年 5 月 6 日)

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

【讨论】:

  • 非常感谢您的提示,嘿嘿。那解决了!请放心,只要有可能,我就会依赖最新的 boost 版本,以免遇到长期解决的问题。但不幸的是,我的嵌入式目标施加了一些限制。
  • @Daniel 哦,好吧,我不能说我完全理解(gcc 似乎足够新)但是无论如何,现在你知道如何找到这些东西了 (start here) :)
  • @sehe:感谢您的链接。最后总结一下:在向 Stack Overflow 社区提出一个问题之前,我想确保该问题并非特定于 TI DSP 编译器。因此我用最近的 g++ 复制了它。
  • 啊。现在它变得更有意义了。下次您可能想在问题中解释这一点,因为我们都是专业人士,我们只会说“duh,使用 gcc 4.8 的最新提升”:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多