主要是因为 C++03 中的一些实现问题,MPL 的作者
必须使用非显而易见的技术来表示序列,其中之一是
像这样的类型的用法
boost::mpl::vector0<>
boost::mpl::vector1<T>
boost::mpl::vector2<T, U>
... etc
而不是简单地写
boost::mpl::vector<>
boost::mpl::vector<T>
boost::mpl::vector<T, U>
就像在 C++11 及更高版本中使用可变参数模板一样。另一种技术
是在向量中插入内容时创建某种反向链接列表,
这就是您在示例中看到的内容:
boost::mpl::v_item<mpl_::int_<8>, // 2nd element
boost::mpl::v_item<mpl_::int_<6>, // 1st element
boost::mpl::vector0<mpl_::na>, 0>, 0> // empty list
正因为如此,文档
for boost::mpl::transform
没有指定exactly是什么类型的boost::mpl::transform<s,op,in>::type
。
实际上,它只保证它是一个等价于的类型
typedef lambda<op>::type f;
typedef lambda<in::operation>::type in_op;
typedef fold<
s
, in::state
, bind< in_op, _1, bind<f, _2> >
>::type r; // <-- the return type is equivalent to this r
除非您已经足够了解 MPL,否则这可能对您没有帮助
你不会问关于它的问题;-),所以基本上这意味着它返回
一种新类型,就像boost::mpl::vector
,除了它的实际类型可能
像我上面展示的那样。特别是,这个类型保证是一个模型
的正向序列概念。
当你使用boost::is_same<T, U>
,你实际上是在问是否T
and U
are 恰恰相同的类型。你现在应该清楚地明白为什么这不是
你确实想要。相反,你想做某种deep那些的比较
两个序列,都代表向量。检查是否有两个Forward
序列相等,必须使用boost::mpl::equal算法代替。以下将起作用:
#include <boost/mpl/assert.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/vector.hpp>
using namespace boost::mpl;
template <int i>
struct multiply_scalar
{
template<typename T> struct apply
{
typedef int_<(T::value * i)> type;
};
};
typedef vector<int_<3>, int_<4> > my_vec;
typedef transform< my_vec, multiply_scalar<2> >::type my_vec_2;
typedef vector<int_<6>, int_<8> > my_vec_3;
BOOST_MPL_ASSERT(( boost::mpl::equal< my_vec_2, my_vec_3 > ));