4

鉴于:

typedef boost::tuple< T1, T2, T3, ..., Tn > Tuple_Tn

其中类型 T1, ... Tn 都已定义,

给定类型 T_another,我想定义一个新的元组类型:

typedef boost::tuple< T1, T2, T3, ..., Tn, T_another > Tuple_T_plus_1

但这是我的问题:在我想定义它的地方,我只能访问类型 Tuple_Tn 和 T_another。

换句话说,是否可以仅根据 Tuple_Tn 和 T_another 来定义 Tuple_T_plus_1?

4

1 回答 1

3

我不确定 Boost.Tuple 中是否有这样的功能,也许Boost.Fusion会更适合您的需求。

但是,如果您有一个支持 C++11 可变参数模板的编译器,您可以切换到std::tuple并编写一个小元函数来将类型附加到现有元组:

template <typename Container, typename T>
struct push_back;

template <template <typename...> class Container, typename T, typename... Args>
struct push_back<Container<Args...>, T>
{
    typedef Container<Args..., T> type;
};

typedef std::tuple<int, double> myTuple;
typedef push_back<myTuple, bool>::type myOtherTuple;

myOtherTuple(1, 0.0, true);

可以实现同样的事情boost::tuple,但编写起来会更加乏味。

于 2012-01-19T17:07:22.160 回答