gpt4 book ai didi

c++ - 为什么在 C++11 中为 std::initializer_list 重载 std::begin() 和 std::end()?

转载 作者:可可西里 更新时间:2023-11-01 17:50:34 26 4
gpt4 key购买 nike

在 C++11(引用 N3337)中,std::begin()std::end() 被指定为(§24.7 [iterator.range ]/p2-3)

template <class C> auto begin(C& c) -> decltype(c.begin());
template <class C> auto begin(const C& c) -> decltype(c.begin());

2 Returns: c.begin().

template <class C> auto end(C& c) -> decltype(c.end());
template <class C> auto end(const C& c) -> decltype(c.end());

3 Returns: c.end().

但是,

std::initializer_list 为这些函数提供了自己的重载(§18.9.3 [support.initlist.range]):

template<class E> const E* begin(initializer_list<E> il) noexcept;

1 Returns: il.begin().

template<class E> const E* end(initializer_list<E> il) noexcept;

2 Returns: il.end().

除了 (1) 具有 noexcept 规范和 (2) 按值获取参数之外,这些重载似乎没有做任何超出基本模板的事情。但是,复制 initializer_list 并没有什么特别的(它只是复制了一对指针或同样轻量级的东西),所以 (2) 在行为上没有差异。此外,许多标准容器的 begin()end() 成员函数也是 noexcept,但没有 std 的重载: :begin()/std::end() 是为这些容器指定的,因此委员会似乎不太可能只为 (1) 指定这些重载。那么,为什么要提供这些重载?

最佳答案

这在 N2930 中有解释,它提议更改以添加有问题的重载。强调我的:

Summary of proposed changes

  • Specify range-based for statements without the use of concepts, using argument-dependent lookup of begin and end (which always includes those begin and end functions in namespace std) to provide the iterator to the beginning and ending of the sequence.
  • Specify range-based for statements so that arrays and initializer lists have no dependencies on <iterator>.
  • Refactor <initializer_list> so that it as no dependencies on other headers and includes no other headers.
  • Specify the library headers that are to #include <initializer_list>.

他们似乎没有解释为什么他们想要<initializer_list>不依赖 <iterator> ,但我认为一个合理的猜测是前者应该在独立实现中可用,而后者可能不可用(表 16,§17.6.1.3)

关于c++ - 为什么在 C++11 中为 std::initializer_list 重载 std::begin() 和 std::end()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25215590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com