Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type of array of const elements

is it specified in standard that array of const elements has different type than array of non-const elements? Here is my code and output of VC2010 and GCC4.8.0.

Thank you.

#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.
like image 980
user2542707 Avatar asked Jul 02 '13 13:07

user2542707


1 Answers

The C++11 standard actually says (3.9.3/1) (emphasis mine)

[...] The cv-qualified or cv-unqualified versions of a type are distinct types; however, they shall have the same representation and alignment requirements

with the following precision:

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

like image 130
JBL Avatar answered Sep 22 '22 02:09

JBL