c++ - const 元素数组的类型

标签 c++ arrays constants standards-compliance

标准中是否规定常量元素数组与非常量元素数组的类型不同?这是我的代码和 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/

相关文章:

c++ - 英特尔 MKL LAPACKE_dsyevr 在 N~8*10^4 的大型 NxN 矩阵上

python - numpy 数组和返回上的 str() 方法

C#:您如何管理项目中的符号常量?您在哪里声明解决方案范围常量?

c++ - 奇怪的错误,set<int>::begin() 总是返回 const 迭代器

java - 是否可以使用 LWJGL 将 C++ OpenGL 代码与 Java 混合?

c++ - 在 C++ 中重载增强赋值(返回类型)

java - Java 中的二维数组未正确更改

c++ - 我什么时候在 C++ 中使用 const volatile、register volatile、static volatile?

c++ - ZeroMQ XPUB/XSUB 严重缺陷?

javascript - 如何将对象重新格式化为数组 Javascript