主要是因爲在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
正因爲如此,在documentation 爲boost::mpl::transform
做沒有指定究竟是什麼是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不夠好, 你不問SO上關於它的問題;-),所以基本上這意味着它返回 一個新的類型,就像boost::mpl::vector
,除了它的實際類型可能是 是我上面顯示的任何東西。特別是,這種類型保證是Forward Sequence概念的型號 。
當您使用boost::is_same<T, U>
,你實際上問 是否T
和U
是正是同一類型。您現在應該清楚地看到爲什麼這不是您實際需要的 。相反,你想做一些深的兩個序列的比較,這兩個序列都代表了載體。要檢查兩個正向序列是否相等,您必須改用算法。以下將起作用:
#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 >));
非常感謝您的回答。 :) – tgmath 2014-11-01 13:46:49