繁体   English   中英

c ++ 03:使用boost的可变参数模板

[英]c++03 : Variadic templates using boost

我正在尝试找到一种解决方法,以在c ++ 03中使用可变参数模板。

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

这就是使用c ++ 11可变参数模板的样子:

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

   Vectors_t _vectorsTuple;
}

我想使用boost :: tuple和boost :: mpl或其他任何可能的方法来达到相同的效果。

谢谢

UPDATE

这是我想出的最终解决方案。 部分基于@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> >{};

这是一种解决方法。 我承认它看起来非常丑陋。 但是我认为这至少是获得某种变奏风格的一种方式:

#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;
}

它的作用是:有一个支持可变参数的宏。 此宏将args传递给mpl::vector FooHelper正在使用mpl::vector<T1,...,Tn>并将其转换为boost::tuple< std::vector<T1>, ..., std::vector<Tn> >

好吧,我想这不是您想要的,但是也许您可以以某种方式使用它。 由于代码块大,我将其作为答案而不是注释。

更新:如果您不喜欢Boost预处理器,则可以通过Boost融合向量获得相同的结果。 引用boost文档: 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM