【问题标题】:Why is there no std::size?为什么没有 std::size?
【发布时间】:2015-02-13 11:27:09
【问题描述】:

Scott MeyersHerb Sutterothers 提倡非成员函数优于成员函数。随着std::beginstd::cend 等的添加,STL 似乎正朝着这个方向发展。如果是这样,为什么没有std::size

我假设是因为定义自己的版本很容易:

namespace std {
    // C++14
    template <typename C>
    decltype(auto) size(const C& c)
    {
        return distance(cbegin(c), cend(c));
    }
}

或者我错过了什么?即是否有理由使用非会员size

【问题讨论】:

  • There is now (好吧,现在还不是)。 Just voted into the working paper last November(见 LWG 动议 20)。
  • 还要指出我的例子std::size不是很好!
  • 你错过了对T[n]的明显支持,是的。
  • @MSalters 并且因为 std::distance 是 O(n),我相信所有 STL 容器都必须有 O(1) size 成员?
  • @Daniel 除了forward_list.

标签: c++ stl c++14


【解决方案1】:

为什么没有std::size

因为没有人及时编写提案以将其包含在 C++14 中。 The first draft of the proposal to add it 日期为 2014 年 5 月,远在 C++14 标准于 2014 年 2 月进行最后一轮投票之后。

提案的A revised versionvoted into the C++ working paper 在 11 月委员会的最后一次会议上(请参阅 LWG 动议 20),因此您很可能会在标准的下一个修订版(C++17?)中看到它,以及std::empty()std::data()

【讨论】:

    猜你喜欢
    • 2015-12-22
    • 2012-01-02
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多