C ++ const used twice in static array declaration

I saw what is constused twice in the array declaration staticbefore and now when I create my own static array. I am wondering why const const will be needed twice in some situations.

Does an array of pointers make a difference?

a. static const TYPE name[5];
b. static const TYPE const name[5];

c. static const TYPE* name[5];
d. static const TYPE* const name[5];

I understand what b.is unacceptable, but if using const twice is valid, what is its purpose?

+5
source share
4 answers

const TYPE * x;

Means that the thing x points to is const.

TYPE * const x;

means that the pointer x is a constant.

Combining 2, you get:

const TYPE * const x;

The value of the pointer and the pointer pointing to it are constants.

+12

cv- (const volatile) , cv, . , , , :

// Let T by any type:
T const tr;
const T tl;
const T const tlr; // only in C
const const const const const T t5; // only in C
typedef const T CT;
CT const tcc; // fine, although CT was already const

, T. T cv-, .

, ; " T":

const T (* tp);

const T* tp;

const , *. , ", T":

T (* const tp) = 0; // must be initialised, because tp is immutable

T* const tp = 0;

[] - , .

+1

const , . ( , .) 5 const TYPE s, .

: const TYPE s, const TYPE s.

, : , .

0

const ++ 2003, ++ 2011 (. 7.1.6.1 [decl.type.cv], 1: " cv- " ).

static const TYPE const name[5];

TYPE. , , ++ 2011, const .

const TYPE

TYPE const

: TYPE . const , , , const (, , ).

Using pointers seems to become different. There are two types of types: a type indicating a type and a pointer. Each of them can be done constseparately:

TYPE const*       ptr1(0);  // non-const pointer to const TYPE
TYPE* const       ptr2(0);  // const pointer to non-const TYPE
TYPE const* const ptr3(0);  // const pointer to const TYPE

The best way to find out what has been done constis to read a type declaration from right to left. Of course, this assumes that qualifiers constare placed in the right place. You can replace constwith volatileor const volatilein the discussion above and use the same reasoning.

0
source

All Articles