4

我有以下代码:

#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 事物(我似乎缺少正确的词)不应该定义相同的类型吗?

谢谢您的帮助。

4

2 回答 2

1

transform输出未指定的序列类型(integral_c<long, n>元素),而不是list_c.

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

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));
于 2012-11-29T18:24:54.920 回答
1

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

于 2012-11-29T18:13:04.967 回答