【问题标题】:BOOST MPL Adding two list_c objects results in unexpected behaviorBOOST MPL 添加两个 list_c 对象会导致意外行为
【发布时间】:2012-11-17 20:56:17
【问题描述】:

我有以下代码:

#include <boost/mpl/list_c.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/assert.hpp>
#include <iostream>
#include <typeinfo>
#include <assert.h>

using namespace boost::mpl;

typedef list_c<long,0,2,4,6,8,10> evens;
typedef list_c<long,2,3,5,7,11,13> primes;
typedef list_c<long,2,5,9,13,19,23> sums;

typedef transform< evens, primes, plus<> >::type result;
BOOST_MPL_ASSERT(( equal< result,sums,equal_to<_1,_2> > ));

int main()
{
    std::cout << typeid(sums).name() << std::endl << typeid(result).name() << std::endl;
    assert(typeid(sums) == typeid(result));
}

它编译,所以 BOOST_MPL_ASSERT 成立。但是,在运行它时,main 函数中的断言失败。这是什么意思?包含相同元素的两个 list_c 事物(我似乎缺少正确的词)不应该定义相同的类型吗?

感谢您的帮助。

【问题讨论】:

    标签: c++ boost transform typedef boost-mpl


    【解决方案1】:

    transform 输出未指定的序列类型(integral_c&lt;long, n&gt; 元素),而不是list_c

    transform 的结果转换为已知类型的一种方法是在list 上使用insert_range

    insert_range<list0<>, begin<list0<> >::type, result>::type
    

    不幸的是,这不会产生 list,而是内部的 mpl::l_item 类型,因此您必须在双方都执行相同的操作:

    BOOST_MPL_ASSERT((is_same<
          insert_range<list0<>, begin<list0<> >::type, result>::type,
          insert_range<list0<>, begin<list0<> >::type, sums>::type>));
    

    同样,您可以在前插入器中使用reverse_copy

    BOOST_MPL_ASSERT((is_same<
          reverse_copy<result, front_inserter<list0<> > >::type,
          reverse_copy<sums, front_inserter<list0<> > >::type
          >));
    

    确实,因为front_inserter 是根据内部元函数push_front 实现的,所以这会产生与insert_range 相同的类型。

    然而,仅仅依靠copy 总是产生相同的未指定序列类型更容易:

    BOOST_MPL_ASSERT((is_same<copy<result>::type, copy<sums>::type>));
    

    由于copy是一种转换算法,它返回的类型与transform相同,所以你可以写:

    BOOST_MPL_ASSERT((is_same<result, copy<sums>::type>));
    
    assert(typeid(copy<sums>::type) == typeid(result));
    

    【讨论】:

    • 谢谢。我无法将列表转换为 list_c 吗?我无法添加两个列表。
    • @user1864035 不,list_c 只是构造 lists 的简写。
    • 嗯,看来我还没说完,恐怕你说的不准确。根据您的建议更改代码,(用您的替换我的断言)导致它无法编译,此外 assert(typeid(sums::type) == typeid(result));失败:(。似乎需要一种将转换的输出序列转换为列表的方法。
    • @user1864035 好电话,我有点困惑。更正了上面的答案。
    【解决方案2】:

    MPL 不对 MPL 算法产生的确切类型做出任何保证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多