c++ - 使用另一个构造一个具体的 boost::tuple 类型

标签 c++ boost boost-tuples

给定:

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/

相关文章:

c++ - 融合 vector 投影

c++ - 提升元组 : increasing maximum number of elements

c++ - 如何将 QT GUI 加入非 C++ 主程序?

c++ - 与 C++11 `enum class` 的 ABI 兼容性保留

c++ - 需要 Boost Pool 经验。作为预分配的分配器有用吗?

c++ - 如何从字节流中提取数据包?

c++ - 为什么不能用兼容类型的 std::tuple 按元素构造 std::tuple?

c++ - 三次贝塞尔曲线交互

c++ - 我在哪里可以找到 cppunit 的版本?

c++ - boost 共享指针构造函数析构函数