codeJack codeJack - 1 year ago 93
C++ Question

c++03 : Variadic templates using boost

I'm trying to find a workaround to use variadic templates within c++03.

What I would like to achieve is - within a templated class - instanciated a

attribute, which would be composed of a vector for each single class template parameter.

This is how it would look like using c++11 variadic templates:

template<typename ...Parameters>
class Foo
typedef std::tuple<std::vector<Parameters>...> Vectors_t;

Vectors_t _vectorsTuple;

I would like to achieve the same using boost::tuple and boost::mpl or whatever other possible ways.


Answer Source

Here is some kind of workaround. I admit that it looks totally ugly. But I think this is at least a way to get some kind of variadic style:

#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/facilities/intercept.hpp>

#include <boost/tuple/tuple.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/copy.hpp> 

#include <vector>

#define Foo(...) FooHelper< boost::mpl::vector<__VA_ARGS__> >

template<class T, class Tuple>
struct tuple_push_front;

template<class T, BOOST_PP_ENUM_PARAMS(10, class T)>
struct tuple_push_front<T, boost::tuple<BOOST_PP_ENUM_PARAMS(10, T)> > {
    typedef boost::tuple<T, BOOST_PP_ENUM_PARAMS(9, T)> type;

template <typename MPLVec>
struct FooHelper {
    typedef MPLVec mpl_vec_t;
    // generate a boost::tuple< std::vector<...>, ... > out of the mpl vector
    typedef typename boost::mpl::fold<mpl_vec_t,
        tuple_push_front<std::vector<boost::mpl::_2>, boost::mpl::_1>
    >::type type;

int main() {
    Foo(int,float,char) test;

What it does is: There is a macro that supports the variadic arguments. This macro passes the args to an mpl::vector. The FooHelper is taking the mpl::vector<T1,...,Tn> and converts it to a boost::tuple< std::vector<T1>, ..., std::vector<Tn> >.

Well, I guess this is not what you want, but maybe you can use it somehow. I put it as answer and not as comment because of the large code block.

UPDATE: If you don't like the boost preprocessor stuff you can get the same result with a boost fusion vector. Quote from the boost doc: The tuple is more or less a synonym for fusion's vector..

#include <boost/fusion/container.hpp>
#include <boost/fusion/sequence.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp>

#include <vector>

#define Foo(...) FooHelper< boost::mpl::vector<__VA_ARGS__> >

template <typename T> 
struct make_vector { 
    typedef std::vector<T> type; 

template <typename MPLVec>
struct FooHelper {
    typedef MPLVec mpl_vec_t;
    typedef typename boost::mpl::transform<mpl_vec_t, make_vector<boost::mpl::_1> >::type vector_types;
    typedef typename boost::fusion::result_of::as_vector< vector_types >::type vectors_t;
    vectors_t _vectorsTuple;

int main() {
    Foo(int, double, float) x;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download