Firstly, the actual problem why your code does not compile is that you are typing operations, not their results. Change it like this:
typedef boost::mpl::push_back<type1, A>::type type2;
typedef boost::mpl::push_back<type2, B>::type type3;
typedef boost::mpl::push_back<type3, C>::type type4;
Now a more general look at why you need typedefs.
(, ) "", "" ++. ++ . .
, , . , :
vector<int> v;
v.push(4);
v.push(7);
v.push(42);
process(&v);
print(v);
:
v = vector<int>;
v1 = v.push(4);
v2 = v1.push(7);
v3 = v2.push(42);
v4 = process(v3);
print(v4);
, , - :
print(process(vector<int>().push(4).push(7).push(42)));
(LISP -) :
(print (process (push (push (push (vector<int>) 4) 7) 42)))
, , .
++ Boost.MPL, , T ( MPL) v - boost::mpl::push_back<v, T>::type , " T ". DRY, typedef " T ".
, , REGISTER_CLASS , . - ( Boost.Preprocessor).
Boost.Preprocessor . REGISTER_CLASS #include d, . - :
internal_register.hpp
#ifndef CLASS_TO_REGISTER
#error You must define CLASS_TO_REGISTER before executing REGISTER_CLASS()
#endif
typedef boost::mpl::push_back<
CURRENT_TYPELIST(),
CLASS_TO_REGISTER
>::type BOOST_PP_CAT(registered, BOOST_PP_INC(BOOST_PP_SLOT(1)));
#undef CLASS_TO_REGISTER
#define BOOST_PP_VALUE BOOST_PP_SLOT(1) + 1
#include BOOST_PP_ASSIGN_SLOT(1)
registration.hpp
typedef boost::mpl::vector<>::type registered0;
#define BOOST_PP_VALUE 0
#include BOOST_PP_ASSIGN_SLOT(1)
#define REGISTER_CLASS() "internal_register.hpp"
#define CURRENT_TYPELIST() BOOST_PP_CAT(registered, BOOST_PP_SLOT(1))
main.cpp ( ):
#include "registration.hpp"
class A {};
class B {};
class C {};
#define CLASS_TO_REGISTER A
#include REGISTER_CLASS()
#define CLASS_TO_REGISTER B
#include REGISTER_CLASS()
#define CLASS_TO_REGISTER C
#include REGISTER_CLASS()
template <typename T> struct wrap {};
struct Print
{
template <typename T> void operator()( wrap<T> t ) const
{
cout << typeid( T ).name() << endl;
}
};
int main()
{
boost::mpl::for_each<CURRENT_TYPELIST(), wrap<boost::mpl::placeholders::_1> >( Print() );
return 0;
}
, ( , , ). .