c++ - 为什么 std::array::front 和 std::array::back 不是 noexcept?

标签 c++ arrays c++11 c++14 noexcept

我不熟悉 noexcept 说明符的使用,我不明白为什么 std::array::frontstd::array: :back 未声明 noexcept (而 std::array::beginstd::array::end 是) .

这是什么原因?

最佳答案

来自 cppreference

There is a special case for a zero-length array (N == 0). In that case, array.begin() == array.end(), which is some unique value. The effect of calling front() or back() on a zero-sized array is undefined.

所以既然我们可以有一个 0 大小的数组 front()back() 可能会导致异常

引用 Sebastian Redlwhy the standard doesn't mandate operator[], front and back be marked noexcept

The standard's policy on noexcept is to only mark functions that cannot or must not fail, but not those that simply are specified not to throw exceptions. In other words, all functions that have a limited domain (pass the wrong arguments and you get undefined behavior) are not noexcept, even when they are not specified to throw.

关于c++ - 为什么 std::array::front 和 std::array::back 不是 noexcept?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32635659/

相关文章:

c++ - std::unordered_map::insert 什么时候会失败?

c++ - 为什么 "n"在此函数中没有递增?

c++ - 使用 g++ 使用 mosquitto 库编译 cpp 代码时出错

c# - 动态创建数组并在 C# 中比较它们

java - 无法让 A 类中的数组在 B 类中工作

c++ - 计算任意多边形的符号距离变换

c++ - 通过初始化列表实例化抽象类

c++ - 用于各种 b 的稀疏带状 Ax=b 的批量 CUDA 解决方案

c++ - 动态删除代码?

java - 如何使数组相互交互?