From: James Kanze on
Frederick Gotham wrote:
> Greg Herlihy:

> int i;

> const int* p = &i;

> Is "p" not a "pointer to const" because i is non-const? Please.

The real problem is elsewhere. If p is a const pointer above,
then what is p in:

int *const p = &i ;

?

Regretfully, common usage is that the first p is a const
pointer (and that nobody has heard of the second). Most of the
time, the context makes what is really meant clear, and we live
with it (since neither you nor I have enough influence to change
general use). When we do need to distinguish, or if there is
any chance of ambiguity, of course, the first is pointer to
const, and the second is const pointer. But in my experience,
such cases are rare.

Logical use of English would of course apply the same
distinction to references (except that there would be no
non-const references). But "logical use" and "English" (or any
other natural language) probably form an antilogy. And while we
should certainly strive to do the best we can, the final goal is
communication, and not some logical ideal.

(Note that I'm not arguing that it should be this way. Only
that it is.)

--
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: Greg Herlihy on
Frederick Gotham wrote:
> Greg Herlihy:
>
> > A const reference is no misnomer:
> >
> > int i;
> > const int& iRef = i;
>
>
> What you have there is a "reference to const" -- a reference cannot be
> declared as const.

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.

> > iRef is clearly a "const reference" - and not a "reference to const"
> > because the object iRef refers to, i, is not const.
>
>
> int i;
>
> const int* p = &i;
>
> Is "p" not a "pointer to const" because i is non-const? Please.

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.

The qualifier 'const' whether it is applied to a pointer or a reference
variable means that that variable cannot be used to assign a new value
to its object. So a "const reference" is a reference variable that
cannot be assigned a new value (because "const" appeared in its
declaration). A "reference to const" is therefore a particular kind of
const reference - one whose aliased object has also been declared
const. So stated simply: a "reference to const" variable has an
immutable value while a "const reference" variable may or may not have
an immutable value.

The latest draft of the C++ Standard uses "const reference" or
"non-const reference" a combined total of nine times, and "reference to
const" just twice. So there is scant support in the C++ Standard for
the view that "const reference" is some kind of a misnomer.

Greg


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

From: Frederick Gotham on
James Kanze:

>> int i;
>
>> const int* p = &i;
>
>> Is "p" not a "pointer to const" because i is non-const? Please.
>
> The real problem is elsewhere. If p is a const pointer above,
> then what is p in:
>
> int *const p = &i ;
>
> ?


A const pointer: T *const p;

A pointer to const: T const *p;

A const pointer to const: T const *const p;

--

Frederick Gotham

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

From: Gerhard Menzl on
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.

> 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.


--
Gerhard Menzl

Non-spammers may respond to my email address, which is composed of my
full name, separated by a dot, followed by at, followed by "fwz",
followed by a dot, followed by "aero".



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

From: Frederick Gotham on
Gerhard Menzl:

> > Then what is ref in
>
> void f(int const& ref);


That function parameter is a reference to a const int.


> int my_rand();
>
> void g()
> {
> int i = 1;
> int const ci = 2;
>
> f(my_rand() % 2 ? i : ci);


When you say that something is a "reference to const" or a "pointer to
const", you're saying that the pointer cannot be used to alter the target
object. You're _not_saying that the target option is const.

.....but you already knew that, right?

Your example is no different to:

int a;
int const b;

int const *const p = my_rand() % 2 ? &a : &b;


> }
>
> - a reference to const or a reference to non-const? You cannot tell, as
> it depends on runtime conditions.


No, it's a "reference to const", because there's no such thing as a const
reference. There's a such thing as a const pointer:

int *const p;

, but not a const reference:

int &const r; /* Compile ERROR */

--

Frederick Gotham

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