Doing this with the boost preprocessor, as Andres suggested, will work.
I tried, and it is not nice and will not compile efficiently. It is also limited to BOOST_MPL_LIMIT_VECTOR_SIZE. If his method works, then it might be a little cleaner.
classA.h:
#if BOOST_PP_IS_ITERATING friend get_elem<mpl_vector, BOOST_PP_ITERATION()>::type; #else #ifndef SOME_INCLUSION_GUARD_H #define SOME_INCLUSION_GUARD_H #include <boost/preprocessor/iteration/iterate.hpp> #include <boost/mpl/vector.hpp> #include <boost/mpl/int.hpp> #include <boost/mpl/at.hpp> #include <boost/mpl/size.hpp> class Dummy {}; template <int Exists> struct get_elem_i { template <typename V, int N> struct get { //typedef Dummy type; typedef typename boost::mpl::at< V, boost::mpl::int_<N> >::type type; }; }; template <> struct get_elem_i<0> { template <typename V, int N> struct get { typedef Dummy type; }; }; template <typename V, int N> struct get_elem { typedef typename boost::mpl::size<V>::type size; typedef get_elem_i<N < size::value> elem; typedef typename elem::get<V, N>::type type; //typedef Dummy type; }; template <typename mpl_vector> class A { #define BOOST_PP_ITERATION_PARAMS_1 (3, (0, BOOST_MPL_LIMIT_VECTOR_SIZE, "classA.h")) ??=include BOOST_PP_ITERATE() private: int test_; }; #endif // SOME_INCLUSION_GUARD_H #endif
This file includes itself, so be sure to specify the same name as the file in the BOOST_PP_ITERATION_PARAMS_1 bit.
In addition, this code will also cause the 'Dummy' class to be a friend of 'A'.
source share