我想这样做以获得类型/类的列表。但我不能使用 C++11。有什么建议可以将类型附加到模板列表吗?
编辑:我想做的一些代码:
#include <iostream>
#include <typeinfo>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/push_back.hpp>
#include <boost/mpl/for_each.hpp>
using namespace std;
class A {};
class B {};
class C {};
typedef boost::mpl::vector<> type1;
// supposed I'd like to have this as a #define macro so someone can call
// REGISTER_CLASS(A) and push the type into a list
typedef boost::mpl::push_back<type1, A> type2;
typedef boost::mpl::push_back<type2, B> type3;
typedef boost::mpl::push_back<type3, C> type4;
template <typename T> struct wrap {};
struct Print
{
template <typename T> void operator()( wrap<T> t ) const
{
cout << typeid( T ).name() << endl;
}
};
int main()
{
// this doesn't compile because type4 is a sequence of sequence
// supposed, I need to "flatten" this list so it's eqv to vector<A,B,C>
boost::mpl::for_each<type4, wrap<boost::mpl::placeholders::_1> >( Print() );
// second problem is that I'd like to have typedef of 1 typelist only, not
// type1, type2, ... typeN, since I don't know the exact number of classes
return 0;
}