From: Dilip on
Frederick Gotham wrote:
>
> int &const r; /* Compile ERROR */

Just a tiny nit -- this is not a compile error. VC 2005 treats it as
an "anachronism" and ignores the const qualifier on a reference.

{ according to 8.3.2/1 it is ill-formed. -mod }

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: rdilipk on
Dilip wrote:
> Frederick Gotham wrote:
> >
> > int &const r; /* Compile ERROR */
>
> Just a tiny nit -- this is not a compile error. VC 2005 treats it as
> an "anachronism" and ignores the const qualifier on a reference.
>
> { according to 8.3.2/1 it is ill-formed. -mod }

So a conforming compiler should treat it as an error? Then its
probably a bug with VC 2005 because the following lines:

int j = 23;
int& const i = j;

produce:

warning C4227: anachronism used : qualifiers on reference are ignored


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: James Kanze on
rdilipk(a)gmail.com wrote:
> Dilip wrote:
> > Frederick Gotham wrote:

> > > int &const r; /* Compile ERROR */

> > Just a tiny nit -- this is not a compile error. VC 2005 treats it as
> > an "anachronism" and ignores the const qualifier on a reference.

> > { according to 8.3.2/1 it is ill-formed. -mod }

> So a conforming compiler should treat it as an error? Then its
> probably a bug with VC 2005 because the following lines:

> int j = 23;
> int& const i = j;

> produce:

> warning C4227: anachronism used : qualifiers on reference are ignored

Which is fine if they document this as a diagnostic. It's a
diagnosible error, according to the standard. Which means that
a conforming implementation must issue a diagnostic. There's
nothing forbidding the diagnostic from containing the word
"warning" (and nothing requiring it to contain the word error),
and there's nothing forbidding the implementation from going on
and compiling the code. (FWIW: Sun has used the "anachronism"
message for years.)

--
James Kanze (Gabi Software) email: james.kanze(a)gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Gennaro Prota on
On 1 Nov 2006 11:30:06 -0500, James Kanze wrote:

>> So a conforming compiler should treat it as an error? Then its
>> probably a bug with VC 2005 because the following lines:
>
>> int j = 23;
>> int& const i = j;
>
>> produce:
>
>> warning C4227: anachronism used : qualifiers on reference are ignored
>
>Which is fine if they document this as a diagnostic. [...]
>(FWIW: Sun has used the "anachronism" message for years.)

Does this mean that it was legal to cv-qualify a reference at some
point in the C++ history? :-O

--
Gennaro Prota
[To mail me, remove any 'u' from the provided address]

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Greg Herlihy on
Gerhard Menzl wrote:
> Greg Herlihy wrote:
>
> > The following is a "reference to const":
> >
> > const int i = 3;
> > const int& iRefConst = i;
> >
> > and differs from the original example significantly: namely iRefConst
> > has an immutable value whereas iRef does not.
>
> Then what is ref in
>
> void f(int const& ref);
> int my_rand();
>
> void g()
> {
> int i = 1;
> int const ci = 2;
>
> f(my_rand() % 2 ? i : ci);
> }
>
> - a reference to const or a reference to non-const? You cannot tell, as
> it depends on runtime conditions. Since this is about terminology for
> static types, your distinction is meaningless.

When bound to the 2, ref is a "reference to const", when bound to ci,
it is a "reference to a non-const". So on every call to f(), ref is
consistently claiming that its bound object is const - even though we
know that at least on some of the calls, that claim is false. So rather
than buy wholeheartedly into ref's claims, we should stick to what we
know and attribute the "const" to its source - the declaration of ref
itself. So f()'s ref paramter is a "const reference" and we leave the
question of the object's actual const-ness unspecified (unless it can
be determined for certain).

We should do so because there is one thing that we can be sure of: is
that there is another, direct declaration for the ref's object
somewhere else in the program - a declaration that really determines
the constness of object. And ref's own declaration does not affect the
declaration for the actual object. So we should reserve "reference to
const" when we know that the referenced object is const, and use "const
reference" as the shorthand for "a reference to a (supposedly) const
object".

Whether ref's object is actually declared const or not is far from a
meaningless distinction. On the contrary, it directly affects whether
it is safe to cache ref's value or not. And the distinction exists -
whether or not we choose to acknowledge it or nor, or even whether we
can determine it in a particular case. The choice is between a richer
vocabulary that is capable of expressing the difference, rather than a
watered-down terminology that cannot.

> > Does 2/3 equal 3/2 because 2 * 3 equals 3 * 2 ? No, because using
> > multiplication to demonstrate a point about division is just as
> > effective as using pointers to prove a point about references.
>
> Just as a point about arithmetics cannot be used to prove a point about
> indirection.

Pointers use indirection, references use aliasing. A pointer does not
claim to be the object that it points to. A pointer to an int is not an
int. A reference on the other hand makes exactly that claim: a
reference to an int is an int. A reference to a const int is a const
int. Whereas a const reference to an int is only an int object
pretending (under an assumed name) to be const. It should be possible
to express in words all three types of references.

Greg


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]