标准中是否规定常量元素数组与非常量元素数组的类型不同?这是我的代码和 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.
最佳答案
C++11 标准实际上说 (3.9.3/1)(强调我的)
[...] The cv-qualified or cv-unqualified versions of a type are distinct types; however, they shall have the same representation and alignment requirements
具有以下精度:
The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and non-static data members of unions
关于c++ - const 元素数组的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17426476/