首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c++03 :使用boost的各种模板

c++03 :使用boost的各种模板
EN

Stack Overflow用户
提问于 2016-11-18 09:29:13
回答 1查看 1.1K关注 0票数 2

我正在试图找到一个解决办法,以便在c++03中使用各种模板。

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

这就是使用c++11变量模板时的样子:

代码语言:javascript
复制
template<typename ...Parameters>
class Foo
{
   typedef std::tuple<std::vector<Parameters>...> Vectors_t;

   Vectors_t _vectorsTuple;
}

我希望通过使用boost::tuple和boost::mpl或任何其他可能的方式来实现相同的目标。

谢谢

更新

这是我想出的最后一个解决办法。部分是基于@stefanmoosbrugger的提议。

代码语言:javascript
复制
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> >{};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-18 15:03:25

这是某种解决办法。我承认这看起来很难看。但我认为这至少是一种获得不同风格的方式:

代码语言:javascript
复制
#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::vectorFooHelper正在获取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.

代码语言:javascript
复制
#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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40673493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档