给定:
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?
最佳答案
我不确定 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
也可以实现同样的效果,但编写起来会更乏味。
关于c++ - 使用另一个构造一个具体的 boost::tuple 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928180/