use*_*707 6 c++ arrays const standards-compliance
标准中指出const元素数组的类型与非const元素数组的类型不同吗?这是我的VC2010和GCC4.8.0的代码和输出.
谢谢.
#include <iostream>
#include <typeinfo>
#include <ios>
int main(){
int arr_a[] = {1, 2};
int const arr_b[] = {3, 4}; // or const int arr_b[] = {3, 4};
std::cout << typeid(arr_a).name() << "\n";
std::cout << typeid(arr_b).name() << "\n";
std::cout << "Same type: " << std::boolalpha << (typeid(arr_a) == typeid(arr_b)) << ".\n";
}
int [2]
int const [2]
Same type: false.
A2_i
A2_i
Same type: true.
Run Code Online (Sandbox Code Playgroud)
C++11 标准实际上说 (3.9.3/1) (强调我的)
[...] 类型的 cv 限定或 cv 不限定版本是不同的类型;但是,它们应具有相同的表示和对齐要求
具有以下精度:
相同的表示和对齐要求意味着作为函数参数、函数返回值以及联合的非静态数据成员的可互换性