C++ c++;03:使用boost的可变模板

C++ c++;03:使用boost的可变模板,c++,boost,variadic-templates,template-meta-programming,C++,Boost,Variadic Templates,Template Meta Programming,我正试图找到一种在c++03中使用可变模板的解决方法 我想要实现的是在模板化类中实例化一个boost::tuple属性,该属性由每个类模板参数的向量组成 这是使用c++11可变模板时的情况: template<typename ...Parameters> class Foo { typedef std::tuple<std::vector<Parameters>...> Vectors_t; Vectors_t _vectorsTuple; }

我正试图找到一种在c++03中使用可变模板的解决方法

我想要实现的是在模板化类中实例化一个
boost::tuple
属性,该属性由每个类模板参数的向量组成

这是使用c++11可变模板时的情况:

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

   Vectors_t _vectorsTuple;
}
模板
福班
{
typedef std::元组向量;
向量(t)向量(vectorsTuple);;
}
我希望使用boost::tuple和boost::mpl或其他任何可能的方法实现同样的效果

谢谢

更新

这是我提出的最终解决方案。 部分基于@stefanmoosbrugger命题

template<class TypesList>
class FooImpl
{
    typedef TypesList TypesList_t;
    typedef typename boost::mpl::transform <
      TypesList_t,
      std::vector<boost::mpl::_1>
      >::type VectorTypesList_t;
    typedef typename boost::mpl::reverse_fold<
      VectorTypesList_t,
      boost::tuples::null_type,
      boost::tuples::cons<boost::mpl::_2, boost::mpl::_1>
      >::type VectorsTuple_t;

     protected:
         VectorsTuple_t _vectorsTuple;
};

template<class Type1, 
         class Type2 = boost::mpl::na, 
         class Type3 = boost::mpl::na,
         class Type4 = boost::mpl::na> /* Made it up to four possible types as an example */ 
class Foo : public FooImpl<boost::mpl::vector<Type1, Type2, Type3, Type4> >{};
模板
类FooImpl
{
typedef TypesList TypesList\t;
typedef typename boost::mpl::transform<
打印列表,
向量
>::类型VectorTypesList\u t;
typedef typename boost::mpl::反向折叠<
矢量类型列表,
boost::tuples::null\u类型,
boost::tuples::cons
>::输入向量;
受保护的:
矢量塔;
};
template/*以四种可能的类型为例*/
类Foo:publicfooimpl{};

这里有一些解决方法。我承认它看起来很难看。但我认为这至少是一种获得某种可变风格的方法:

#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,
        boost::tuple<>,
        tuple_push_front<std::vector<boost::mpl::_2>, boost::mpl::_1>
    >::type type;
};

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

谢谢,这还不错:)从mpl::vector到实际元组的转换有没有可能不那么难看?我们真的需要BOOST_PP_ENUM宏和自定义push_前端吗?想知道是否可以使用mpl::transform而不是foldwell以更平滑的方式实现这一点,您可以使用
mpl::transform
上的
mpl::transform
将元素从
Tx
转换为
std::vector
,然后从中创建一个boost融合向量(
boost::fusion::result\u of::as\u vector::type
).. :) 看起来肯定不会那么难看。你能用这个解决方案更新你的代码吗?:)转换的输出仍然需要是boost::tuple,而不是boost融合向量。现在看起来简单多了(如预期的那样)。:)只要看看代码。要回答关于元组的问题,请参阅boost文档中的引用。
#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;
}