【问题标题】:At it's core, how is boost tuple implemented (without all the extra details in the boost header)它的核心是如何实现 boost 元组(在 boost 标头中没有所有额外的细节)
【发布时间】:2012-05-23 15:17:22
【问题描述】:

它似乎是一个包含 10 个东西的硬编码模板,但也有一个与 Lokki 的 Typelist 非常相似的 cons 模板类的实现。那么 boost tuple 模板是否只是 Typelist/cons 实现的包装器?或者它的要点是什么?

【问题讨论】:

标签: c++ templates boost


【解决方案1】:

在 C++03 中(否则我们将谈论 std::tuple)没有可变参数模板功能,因此它使用足够数量的参数 (*) 和特定默认值进行模拟。

您可以通过阅读Design decisions rationale 获得实际实现的提示:

cons 列表的结束标记(nil, null_type, ...)

元组在内部表示为 cons 列表:

tuple<int, int>

继承自

cons<int, cons<int, null_type> >

null_type 是列表的结束标记。原提案是nil, 但是这个名字是在 MacOS 中使用的,可能会引起问题,所以 而是选择了null_type。考虑的其他名称是null_tunit(SML 中的空元组类型)。

注意null_type 是一个空元组的内部表示: tuple&lt;&gt; 继承自 null_type

(*) 通常在 Boost 中,代码是使用宏生成的,因此上限是可配置的。

【讨论】:

    猜你喜欢
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2011-01-11
    • 2019-12-25
    • 2011-05-11
    相关资源
    最近更新 更多