是否在标准中指定常量元素数组的类型与非常量元素数组的类型不同?这里是我的代码和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.
GCC的'typeid'好像忽略了顶级的'const',但不信任它。 –
@gx_相信它! 5.2.8/5会忽略顶级'const'。问题是,如果'const int a [2]'具有**顶级'const' **。 – MWid
是的,'const int a [2]'是一个_cv-qualified_类型(见8.3.4和3.9.3)。因此,gcc的输出是正确的。但'arr_a'和'arr_b'的类型是不同的。如果您想区分与cv-qualifiers相关的类型,则不能使用'typeid'。 – MWid