#include #include #include #include #include #include namespace mpl = boost::mpl; class Base { public: virtual void m() = 0; virtual ~Base() {} }; class Factory { public: typedef Base* (*Creator)(); void register_f(const char* name, Creator f) { m_map[name] = f; } Base* create(const char* name) { return m_map[name](); } static Factory& instance() { static Factory f; return f; } private: Factory() {} std::map m_map; }; template struct Helper { Helper(const char* name) { Factory::instance().register_f(name, &Helper::create); } static Base* create() { return new T; } }; template class DerivedUVW : public Base { public: DerivedUVW() {} virtual void m(){} }; struct A1 {}; struct A2 {}; // for U struct B1 {}; struct B2 {}; // for V struct C1 {}; struct C2 {}; // for W // helper macro (do not repeat in the same line!) #define HELPER_DERIVED_UVW(U, V, W) \ Helper > BOOST_PP_CAT(h, __LINE__) (#U #V #W); // Manual registration: // must type all possible combinations -> ugly HELPER_DERIVED_UVW(A1, B1, C1); HELPER_DERIVED_UVW(A1, B1, C2); // ... // Automatic registration: // generate all possible combinations with MPL+fusion -> howto? // .... typedef mpl::vector Ulist; typedef mpl::vector Vlist; typedef mpl::vector Wlist; int main() { assert( Factory::instance().create("A1B1C1") ); return 0; }