[This answer addresses C++98/03 only; for modern code, see below]
The number of template parameters of boost::variant
is given by BOOST_VARIANT_LIMIT_TYPES
. You could use it by harnessing Boost.Preprocessor:
#include "boost/preprocessor/repetition/enum_params.hpp"
struct Foo {
private:
Foo();
template <BOOST_PP_ENUM_PARAMS(BOOST_VARIANT_LIMIT_TYPES, class T)>
friend class boost::variant;
};
boost::variant
is declared as a class template with BOOST_VARIANT_LIMIT_TYPES
template parameters, so you must refer to it as such. This is the job for BOOST_PP_ENUM_PARAMS(a, b)
, which expands into a list of a
comma-delimited items, each of which is b
with a unique number appended. For example,
BOOST_PP_ENUM_PARAMS(5, class T)
will expand to:
class T0, class T1, class T2, class T3, class T4
Note that the above applies within the scope of the question, that is, limited to C++98/03. Since C++11, variadic templates exist and when these are available to Boost, BOOST_VARIANT_LIMIT_TYPES
is not defined and the above code does not work. Boost offers an alternative which works both with and without variadics; see this answer for details.