我有一个任务:
给定一系列类型。检查它们是否都是类并排列它们,以便派生类在它们的基类之前。对于结果序列,检查所有类是否都派生自最后一个。
您需要使用 boost::mpl 来实现该解决方案。现在我有一个 Swap,它按 mpl::vector 的顺序交换 2 个元素:
template< typename Seq, typename First, typename Second >
struct swap {
private:
typedef typename begin<Seq>::type begin;
typedef typename end<Seq>::type end;
typedef typename clear<Seq>::type empty_container;
// Insert values from begin to first
typedef typename
copy<
iterator_range< begin, First >,
back_inserter< empty_container >
>::type prefix;
// Insert second value
typedef typename
push_back<
prefix, typename
deref< Second >::type
>::type prefixSecond;
// Insert values from first+1 to second
typedef typename
copy<
iterator_range< typename next< First >::type, Second >,
back_inserter< prefixSecond >
>::type prefixSecondMiddle;
// Insert first value
typedef typename
push_back<
prefixSecondMiddle, typename
deref< First >::type
>::type prefixSecondMiddleFirst;
// Insert values from second+1 to end
typedef typename
copy<
iterator_range< typename next< Second >::type, end >,
back_inserter< prefixSecondMiddleFirst >
>::type prefixSecondMiddleFirstSuffix;
public:
typedef prefixSecondMiddleFirstSuffix type;
};
这是一个使用示例:
typedef typename boost::mpl::next<myvector::begin>::type first;
typedef typename boost::mpl::next<first>::type second;
typedef swap<myvector, first, second>::type res;
还有一个谓词检查第一个元素是否是第二个元素的父元素:
template< typename T1, typename T2 >
struct compare_two{
static const bool value = is_base_of<T1, T2>::value;
};
现在,假设我们有一个类型列表:
class base1 {};
class child1 : base1 {};
class child2 : child1 {};
class child3 : child2 {};
typedef vector<child2, child1, base1, child3> vec;
有必要在应用元函数后变成:child3,child2,child1,base1
据我了解,如果第一个是第二个的祖先,您只需要对向量进行排序,在地方更改相应的类型。告诉我如何使用 Boost MPL 来实现它?