使用boost::mpl模块实现partition功能
在C++中,我们经常需要将一个类型序列分成两个部分:一部分满足某些条件,而另一部分则不满足。这种操作在元编程中非常有用,可以通过这种方式对类型序列进行过滤和处理。Boost库中的mpl模块提供了一个非常方便的实现方式。
下面是一个使用mpl实现partition的测试程序:
#include <boost/mpl/vector.hpp>
#include <boost/mpl/partition.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/static_assert.hpp>
#include <iostream>
using namespace boost::mpl;
using namespace std;
// 创建一个类型判断器
template <typename T>
struct is_even : public bool_<T::value % 2 == 0> {};
int main()
{
typedef vector<int, char, short, double, long, float> types;
typedef partition<types, is_even<_1>