boost::mpl::identityとboost::type


#include <iostream>
#include <[]boost[]/mpl/identity.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/type.hpp>

namespace pst {
namespace mpl = boost::mpl;

struct my_print_type {
// mpl-style
template <typename T>
void operator()(mpl::identity<T>) const {
std::cout << typeid(T).name() << std::endl;
}
// []boost[]-style
template <typename T>
void operator()(boost::type<T>) const {
std::cout << typeid(T).name() << std::endl;
}
};

void test_print_type() {
// mpl::for_each needs value-initialize-able types, but...
typedef mpl::vector<int&, long&, char*&> t_nonvalue_types;

// so wrap a type with mpl::identity<type> which is value-initialize-able
mpl::for_each< t_nonvalue_types, mpl::make_identity<mpl::_1> >(my_print_type());
// you can use boost::type which behaves like a Metafunction returning itself
mpl::for_each< t_nonvalue_types, boost::type<mpl::_1> >(my_print_type());
}
}

C++ Template Metaprogramming Section 9.1.1