c++ - std::cbegin() 除了 begin() 一个 const 引用之外还有别的吗?

标签 c++ c++11 iterator

我不得不编写 C++11 代码,但我想使用 std::cbegin()。所以,我正在查看 GCC 5.4.0 的实现,在我的系统上的 /usr/include/c++/5/bits/range_access.h 中,我想我可能会写一些类似的东西,我看到:

  template<class _Container>
    inline constexpr auto
    cbegin(const _Container& __cont) noexcept(noexcept(std::begin(__cont)))
      -> decltype(std::begin(__cont))
    { return std::begin(__cont); }

仅此而已吗?我错过了什么吗?如果是这样,为什么它不像 std::begin() 那样是 C++11 的一部分?

最佳答案

is that all there is to it?

是的。

Am I missing something?

我不知道。

If that's it, how come it wasn't part of C++11 like std::begin()?

全局模板似乎是原始提案的一部分,作为成员函数的替代方案,但该提案更倾向于仅提供成员函数,以支持仅提供全局模板,或同时提供模板和成员。 (假设这是原始提案:N1674)。

委员会选择在 C++11 中包含成员函数选项,而在 C++14 中才包含模板。我不是委员会的成员,不能代表他们发言,但我的猜测是提案的态度可能影响了决定:

While this generic adapter alternative seems quite straightforward, we nonetheless favor the member function approach as proposed above. It seems more in keeping with current C++ programming idioms, such as the parallel use of rbegin as a container member function rather than as a generic adapter

这是 C++ 标准库缺陷报告的开发 issue (2128)毕竟,模板版本决定采用 C++14。

关于c++ - std::cbegin() 除了 begin() 一个 const 引用之外还有别的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41527933/

相关文章:

c++ - 使用scoped_ptr解压可变参数模板

c++ - Bison C++ - 减法

C++:为什么为字符串定义了 'operator+=' 而不是 'operator+'?

c++ - 安全地迭代 std::vector 而项目可能被删除

c++ - 对 vector 中的增量值

c++ - 将几个 std::list 迭代器压缩在一起

c++ - 类对象的模板参数

c++ - std::pair move 没有在定义中被省略?

c++ - 删除 std::multiset 中的元素导致不相关的迭代器无效

c++ - 从 cmake 在 VS 2013 中启用 C++11 支持