3

Given

template< class Type >
void constref( Type const ) {}

void constref_call() { double x; constref<double&>( x ); }      // OK

template< class Type >
using reference = Type&;

void foo( reference< const int > const x ) { (void) x; }        // OK

template< class Type >
void foot( reference< const Type > arg ) { (void) arg; }

void foot_call() { foot( 3.14 ); }      // Deduces arg type no problem.

void foo2( int const& const x ) { (void) x; }                   // !

With both Visual C++ and g++ this code compiles as indicated in the comments, with only foo2 provoking a compilation error.

I would have liked foo to similarly result in compilation error, in order to be able to use that notation with the same constraints as the core language's “failed experiment” operator notation.

I suspect that the reason that foo compiles is the same as the reason why the call in constref_call compiles, some exemption to do with templates, but is that really so – what are the standard's formal rules here?

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 2
    Rule is [dcl.ref]/p1 "Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a *typedef-name* (7.1.3, 14.1) or *decltype-specifier* (7.1.6.2), in which case the cv-qualifiers are ignored." – David G Mar 17 '15 at 15:10
  • In English: References are always const. You can't change them like pointers. – Fozi Mar 17 '15 at 15:21
  • `-Wignored-qualifiers` works for `clang` – Shafik Yaghmour Mar 17 '15 at 15:35
  • 3
    @anonymous downvoter: I suspect you're one more of the serial downvoters, but if not then please do **explain** your downvote. If you feel that you can't explain a vote, then please do abstain from voting. – Cheers and hth. - Alf Mar 17 '15 at 15:37

1 Answers1

4

Quoting C++11, 8.3.2/1:

... Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef (7.1.3) or of a template type argument (14.3), in which case the cv-qualifiers are ignored. ...

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455