Syntax to determine const array type

Be careful, I'm just interested in the possibility of C ++ syntax, and not in practical use.

It is easy to determine the type of array. As an example, int a[3];determines the type of array 3 int, const int a[3];or int const a[3];determines the type of array 3 const int. None of the three forms actually defines a const array of some type T(which itself can be const-modified, of course). Therefore, the following code will not compile:

void f(int (&a)[3]) {...}

f({1, 2, 3});

The reason is simple: the reference is not a constant lval cannot be bound to a temporary rval. One way to fix the code:

typedef int ArrOfInt[3];

void f(const ArrOfInt& a) {...}

f({1, 2, 3});

: ++ const, typedef ?

+4
2

cv- , -, . ,

cv-, , , (8.3.4).

([basic.type.qualifier]/2 N3936)

, , , :

... , , cv-qual CV- .

([basic.type.qualifier]/5)

, , typedef.

void f(const int (&a)[3]);

const - , const, lvalue const. , .

+5

# 1059:

, , , ; [...]

..., ++ 11. [basic.type.qualifier]/5 :

Cv-, , , "cv T", T - , , . , cv, , cv-qualifications .

, const, , .
.

const int arr[2];

arr ( ) const (std::is_const<decltype(arr)>{} is true), .

void f(const (&a)[3]) {...}

a const const.

+1

All Articles