0

I know that

f(const T& obj) // (1)
g(T const& obj) // (2)

are the same .( We can't change the value of obj in the body of f and g ).

But what

h(T & const) // (3)

realy means ?

Another example :

k(T const * const & const obj_p) // (4)

first const : You can not change the value of actual obj (*obj_p)

second const: You can not change the value of * (obj_p)

third const : ?

amin
  • 3,672
  • 5
  • 33
  • 61
  • 4
    That shouldn't compile to my knowledge, and [indeed it doesn't](http://coliru.stacked-crooked.com/a/b1a5b2e8c7c38ab4). – chris Mar 17 '14 at 13:10
  • 2
    Read it right-to-left. And compiler won't let you compile `T & const`, because references themselves cannot be changed, so it's redundant to make reference itself `const`. – lapk Mar 17 '14 at 13:11
  • 2
    There is an entire FAQ about constness at http://www.parashift.com/c++-faq-lite/const-correctness.html – Marius Bancila Mar 17 '14 at 13:11

2 Answers2

1

Having the const after the & means it binds to the variable name so suggests that the name cannot be made to refer to something else after initialization. But that's true of all references anyway, so even if your compiler accepts having & const, it is superfluous and would be more clear as just &.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

The third example:

h(T & const) 

does not really compile.

The fourth example:

k(T const * const & const obj_p)

does not compile either, but if you remove the last const qualifier:

k(T const * const & obj_p)

it is a const pointer (the pointer address cannot be changed) to a const T (the pointed object cannot be changed) passed as a reference.

For more informations see this answer.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272