samedi 28 janvier 2017

Why isn't it valid to declare const references to const non-pointer-types?

My confusion is based on the following function declarations:

foo(int const &) // (1) -- compiles
foo(const int &) // (2) -- compiles 
foo( const int * const & ) // (3) -- compiles 
foo( const int const & ) // (4) -- does _NOT_ compile

Question 1

Assuming the declarations of (1) and (2) are equal: do they take a parameter of type non-const reference to an const int or const reference to an non-const int?

Question 2

Why is it valid to declare a const reference to a const int * but not valid to declare a const reference to a const int?


Log from gcc-compiler version 5.4.0 compiling prototype (4):

error: ‘const’ qualifiers cannot be applied to ‘const int&’
foo ( const int & const a ) 

Aucun commentaire:

Enregistrer un commentaire