为什么交换多维数组不是例外
Why is swapping multidimensional arrays not noexcept?
我有以下片段:
#include <algorithm>
#include <iostream>
int main(int argc, char** argv) {
int x[2][3];
int y[2][3];
using std::swap;
std::cout << noexcept(swap(x, y)) << "n";
return 0;
}
使用GCC 4.9.0打印0
。我不明白为什么。
根据标准,std::swap
有两个过载:
namespace std {
template<class T> void swap(T& a, T& b) noexcept(
is_nothrow_move_constructible<T>::value &&
is_nothrow_move_assignable<T>::value
);
template<class T, size_t N>
void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
}
根据我的理解,数组的noexcept
说明符应该递归地用于多维数组。
为什么交换多维数组不是noexcept
?
在试图找到一个仍然表现怪异的最小例子时,我想出了以下方法:
#include <iostream>
template<class T> struct Specialized : std::false_type {};
template<> struct Specialized<int> : std::true_type {};
template<class T> void f(T& a) noexcept(Specialized<T>::value);
template<class T, std::size_t N> void f(T (&a)[N]) noexcept(noexcept(f(*a)));
int main(int argc, char** argv) {
int x, y[1], z[1][1];
std::cout << noexcept(f(x)) << " "
<< noexcept(f(y)) << " "
<< noexcept(f(z)) << "n";
}
使用GCC 4.9.0打印1 1 0
,但我也不明白为什么。
此过载:
template<class T, size_t N>
void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
直到;
才在作用域中,因此swap(*a, *b)
不考虑此过载。这是因为:
3.3.2/1名称的声明点在其完整声明符(第8条)之后,在其初始值设定项(如有)之前…
异常规范是声明器的一部分。
相关文章:
- 通过交换元素使数组相同
- 如何在数组中交换最小和最大的位置?
- 交换函数不是在 C++ 中交换 2D 数组的元素
- 如何交换数组中的元素?
- C++:交换矩阵的行:使用单维数组
- 使用指针 c++ 交换结构数组中的元素
- 使用指针交换数组的内容
- C++数组交换并查找最小元素
- 使用指针交换而不引用数组C++
- 指针数组交换无需直接访问数组
- 交换数组
- 如何在C++中交换对象数组的 2 个不同对象
- 如何将元素的索引从2D数组存储到1D数组中,然后交换这些值
- 在C++中使用 2D 数组进行交换
- 代码是否交换数组的两个数字
- std::在 std::shared_ptr 之间交换,<A>其中 A 具有动态数组
- 在2D数组中交换行的最佳方法
- 如何在 2D 数组 c++ 中交换元素
- 交换二维数组中的两个值
- 如何使用数组交换对象,c++.我想改变