1

8.3.2/1:

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-specificer (7.1.6.2), in which case the cv-qualifiers are ignored

int a = 5;
const int &b = a;

int main()
{
}

Compiles fine by both gcc and clang. DEMO

Why? Is it a bug?

Example, provided by the Standard:

typedef int& A;
const A aref = 3; // ill-formed; lvalue reference to non-const initialized with rvalue
  • I think it refers to references like `int &const b = a;`. Why do *you* think this is a bug, btw? – Ulrich Eckhardt Sep 28 '14 at 08:33
  • 1
    `const inst&` is not a cv-qualified reference, it's a reference to a cv-qualified int. – Mat Sep 28 '14 at 08:35
  • Maybe it's easier to understand if you say that `const int*` (or as I'd write it `int const*`) is not a constant pointer to an int but a pointer to a constant int. A constant pointer would be `int* const`. – Ulrich Eckhardt Sep 28 '14 at 08:38
  • @UlrichEckhardt Are you sure that the decalration `int &const b = a;` even make a sense? I think, no. It's due to a decalrartor of a reference has the form `& attribute-specifier-seqoptD1`. `attribute-specifier-seq` cannot be const. –  Sep 28 '14 at 08:39
  • @Mat So what is the point of the example the Standard provides? I've added one to my Q. –  Sep 28 '14 at 08:42
  • 1
    @Dmitry: that example "expands into" `int& const aref = 3;` and the const is dropped due to the rule you quote. (Just to be clear: `int const&` and `const int&` are the same thing, but `int& const` is a completely different beast that you are not allowed to write directly.) – Mat Sep 28 '14 at 08:45
  • 1
    You are right, @DmitryFucintv, the declaration `int &const b = a;` doesn't make sense, and it is the paragraph which you quoted from the standard that says so: "Cv-qualified references are ill-formed"! – Ulrich Eckhardt Sep 28 '14 at 09:05
  • @Mat Ah, indeed. Thank you. Your explanation is clear. –  Sep 28 '14 at 09:13

1 Answers1

0

In your example you have no cv-qualified reference. The quote of the Standard means the following declarations that are ill formed

int a = 5;
const int & const b = a;

Here b is a cv-qualified reference and the code snippet is ill-formed.

As for your code snippet then it declares a reference to a const object.

This code snippet is ill formed

typedef int& A;
const A aref = 3; 

because it is equivalent to

int & const aref = 3; 

That it would be more clear compare the two declarations

int x;
int * const p = &x;
int & const r = x;

The declaration of the cv-qualified pointer is valid while the declaration of the cv-qualified reference is ill-formed.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • In your first snippet, you have two `const`, but the first one on the left is superfluous. Your second snippet is invalid because it creates a reference to non-const int from an integer constant (a literal). Concerning that one, isn't the use of a typedef-name exactly what is allowed as exception, in which case the cv-qualifier is ignored? – Ulrich Eckhardt Sep 28 '14 at 09:02
  • @Ulrich Eckhardt it is not superfluous. I took the example from the original post and added one more qualifier that to show what the Standard is speaking about. – Vlad from Moscow Sep 28 '14 at 09:05
  • I called it superfluous because the snippet is only invalid due to the rightmost `const`, the first one doesn't matter. But anyhow, thi is a matter of taste. – Ulrich Eckhardt Sep 28 '14 at 09:09
  • Could you clarify one thing? Are `const A b;` and `A const b;` fully equivalent? –  Sep 28 '14 at 09:14
  • If so, then I've understood what the Standard is trying to say. –  Sep 28 '14 at 09:15