Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constant elements vs. constant array

Everybody knows how to declare an array with constant elements:

const int a[10];

Apparently, it is also possible to declare an array that is itself constant, via a typedef:

typedef int X[10];
const X b;

From a technical and a practical standpoint, do a and b have the same type or different types?

like image 409
fredoverflow Avatar asked Jan 07 '23 00:01

fredoverflow


2 Answers

Apparently, it is also possible to declare an array that is itself constant

Nope. In N1256, §6.7.3/8:

If the specification of an array type includes any type qualifiers, the element type is so-qualified, not the array type.118)

Then footnote 118 says:

Both of these can occur through the use of typedefs.

like image 176
uh oh somebody needs a pupper Avatar answered Jan 13 '23 02:01

uh oh somebody needs a pupper


They have the same type, though clang will print them differently. Since the array itself cannot be const, it is said to "fall through".

For the non-typedefed version, clang prints the type as const int [10]. For the typedef version, it prints int const[10]. Both of those are equivalent.

Coliru in action.

like image 40
Ven Avatar answered Jan 13 '23 01:01

Ven