1

In C++ Primer, it is mentioned that, auto ordinarily ignores top-level constants. As usual, in initializations, low-level constants, such as when initializing a pointer to a constant are kept.

const int ci = i, &cr = ci; 
auto b = ci; // ok:b is an int (top level constants in ci is ignored)
auto d = &i; // d is an int* (& of a int object is int *)
auto e = &ci; // e is a const int *(& of a constant object is a low level constant)

Now, my question is: In the 2nd statement, const is ignored and type of b is int. But in the last statement, the const of ci is not ignored and the type is const int*, rather than int*. Why?

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
  • Show declaration of `i` – M.M Jun 28 '16 at 05:12
  • you answered your own question, after `&ci` the `const` is one level of indirection away, but only top-level consts are ignored – M.M Jun 28 '16 at 05:12
  • int i; What i know is, if the object is itself constant, the constant is top-level , otherwise low level. I don't know exactly what these terms mean. I didn't get your statement, "one level of indirection away"? – Naman Sharma Jun 28 '16 at 05:20
  • "level" means "level of indirection" in all this – M.M Jun 28 '16 at 05:27
  • What does that mean exactly. – Naman Sharma Jun 28 '16 at 05:32
  • See [What are top-level qualifiers?](http://stackoverflow.com/questions/7914444/what-are-top-level-const-qualifiers) – M.M Jun 28 '16 at 05:38
  • Also, [Where is the definition of `top-level cv-qualifiers` in the C++11 Standard?](http://stackoverflow.com/questions/24676824/where-is-the-definition-of-top-level-cv-qualifiers-in-the-c11-standard) – R Sahu Jun 28 '16 at 05:41

4 Answers4

3

When you use auto b=ci;, you create a copy of ci.So C++ has no reason to prevent you from changing the value of b. But if you use auto e=&ci;, you will create a pointer of const int variable ci. e should be a pointer on a constant value to prevent you from changing the value of ci.

Null
  • 657
  • 2
  • 6
  • 15
1

The use of

const int i = ...;

to define a const object has been a source of confusion for a long time. It makes people think that;

const int* ptr = ...; 

also defines a const pointer. That would be an erroneous conclusion. If you move the const a bit, it is less confusing.

int i = ...;                 // Defines i to be a non-const object
int const i = ...;           // Defines i to be a const object

int* ptr = ...;              // Defines ptr to be a non-const pointer to a non-const object
int const* ptr = ...;        // Defines ptr to be a non-const pointer to a const object
int* const ptr = ...;        // Defines ptr to be a const pointer to a non-const object
int const* const ptr = ...;  // Defines ptr to be a const pointer to a const object

Coming to the issue of top level cv-qualifiers,

int const i = ...;

defines an object whose type is int and it has const qualifier.

int volatile i = ...;

defines an object whose type is int and it has volatile qualifier.

int const* ptr = ...;

defines an object whose type is int const* but it has no const or volatile qualifier. The second-level type, int has const qualifier but not the top level type.

int const* const ptr = ...;

defines an object whose type is int const* and it has const qualifier. The second-level type, int, also has const qualifier.

int * const ptr = ...;

defines an object whose type is int* and it has const qualifier. The second-level type, int, has no const qualifier.

More info:

Where is the definition of `top-level cv-qualifiers` in the C++11 Standard?
What are top-level const qualifiers?

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

It is a low level constant because it is directly referencing it. auto b = ci copies the value of ci to b, but auto e = &ci has to have some const-ness, as it does NOT copy ci, but instead points to where ci exists. The term low level means, in this case, that there is not much indirection.

Darkrifts
  • 211
  • 2
  • 11
0

You forgot to mention int i=0; Here i can change in the context of the program which makes it non-constant.

In the 2nd statement, const is ignored

So,there is no top-level const to be ignored

Actual code in C++ Primer,

int i = 0;//Value of i is 0 but it might change
const int ci = i;//Value of ci will not change once initialized
auto d = &i;//Hence d is an int*(No top-level const)

in the last statement, the const of ci is not ignored

& of a const object is a low-level const which auto doesn't ignore

auto e = &ci;// e is const int*(low-level const)