I have the following situation: suppose I have a bunch of types (functors) which I want to register/compile in during compilation, preferably into something like boost::mpl::vector. Do you know any trick to do so nicely?
My desire is to have hpp file which implements functor type and registration file, where a macro brings in type into compilation.
For example
// registered.hpp
REGISTER("functor1.hpp") // implementation
REGISTER("functor2.hpp")
...
boost::mpl::vector<...> types; // full registration vector
Hopefully it makes sense. Thank you
You'll never solve the mpl::vector idea. You can't alter template "variables". Remember that template metaprogramming is a PURE functional language. No side effects at all.
As for registering...the macro thing works fine. Define the macro so that it declares and initializes some small global variable with the registration process. Alternatively you can go down the road I did here:
How to force inclusion of "unused" object definitions in a library
Note the fix if you're trying to do it in a library.
There is a way to register types one by one and then retrieve all of them in the form of mpl::vector or similar. I've learned this trick on the boost mailing lists (perhaps from Dave Abrahams, although I can't recall for sure).
Edit: I learned it from slide 28 on https://github.com/boostcon/2011_presentations/raw/master/thu/Boost.Generic.pdf.
I won't use MPL in the code to make it self contained.
Usage example:
I wouldn't use the macros. Usual technique is to just define some object whose initialization does the registration. Pitfall: you need to reference something, e.g. call a function, in the compilation unit, in order to have it linked in.
Cheers & hth.,