0
votes

I have a boost::mpl Sequence like this:

typedef boost::mpl::vector<
    char, unsigned char, short, unsigned short, int, unsigned int, float, double
> TTypes;

I have a specific-purpose container class which I want to export from my dll:

template<typename T>
class Container { T* _elements; }

Now what I need in my header (MSVC compiler) is several lines like this:

template class __declspec(dllexport) Container<char>;
template class __declspec(dllexport) Container<short>;

and so on (and I need the same but with dllimport for the clients of this library to include).

Now my question is, is there a way to generate these lines from my mpl vector?

I suspect this isn't possible, so my fallback: is there a way to get the preprocessor to do this for me? So, are there any (boost) macros that will somehow loop over the elements in a Sequence (it's fine if I have to use some special syntax to define my Sequence) so that I can do something with the type names in preprocessor strings?

1
can't think of a way. Looks like a job for boost preprocessor macros unfortunately. - Richard Hodges

1 Answers

1
votes

I'm afraid you can't do this in as a template expansion but you can do it cleanly and DRYly with boost preprocessor.

#include <boost/preprocessor.hpp>

/*
 * define the variants as a tuple
 */
#define VARIANTS (char, unsigned char, short, unsigned short, int, unsigned int, float, double)

/*
 * our enumeration function which defines an export
 */
#define MAKE_EXPORT(r, data, elem) template class __declspec(dllexport) Container<elem>;

/*
 * enumerate cast the tuple to a sequence and enumerate, calling MAKE_EXPORT once for each enumeration
 */
BOOST_PP_SEQ_FOR_EACH(MAKE_EXPORT, _, BOOST_PP_TUPLE_TO_SEQ(VARIANTS))