From: Francis Glassborow on
In article <1162459836.811397.261810(a)m7g2000cwm.googlegroups.com>,
ThosRTanner <ttanner2(a)bloomberg.net> writes
>It was lost when the language allowed 'const int fred' as a synonym for
>'int const fred'. You'd be surprised at the number of people who look
>at me strangely when I write 'int const *fred' rather than 'const int
>*fred', and then look very confused when I explain about a pointer to a
>const int and a const pointer to an int, and a const pointer to a const
>int and point out that my way of writing is more consistent when taken
>over the whole range of possibilities.


Why do people have to add confusion. Because there is no real
possibility of confusion it has become idiomatic in C++ to refer to a
reference to const as a 'const reference'. It can only mean one thing,
no problem of confusion for anyone who is reasonably knowledgeable in
C++,

OTOH pointers are a different thing entirely, a const pointer IS quite
distinct from a pointer to const. Confusing the two terms is a serious
problem. No one from a C background would confuse the terms. They would
understand the difference between the pointer not being modifiable, a
const pointer and the object pointed to no being modifiable (through
this pointer) a pointer to const.

As for the way we write pointers to const, there are good reasons for
preferring:

int const * ptr;

to

const int * ptr;

even though they have identical meanings. I first came across a
carefully worded rationale for this choice from Dan Saks more than a
decade ago. In simple terms it is far too easy to confuse the later with

int * const cptr;

because programmers get used to thinking (correctly) that const atype
and atype const are synonyms. Does this matter, well if no one ever
aliased compound types with typedef provided names it would probably be
relatively innocuous but we do use typedefs for compound types, and some
use them to hide away that a type is a pointer. Some of those also then
confuse the consequences of:

typedef int * intptr;


int i;
const intptr ptr(i);
intptr const ptr(i);

Thinking that the first declares ptr to be a pointer to a const int and
the second declares ptr to be a const pointer to int. Of course both
declare ptr to be a const pointer to int.

If we stop thinking of references as some kind of hidden pointer we have
a better chance of not confusing terminology used for speaking/writing
about references with that used for pointers.




--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects


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

From: Seungbeom Kim on
Francis Glassborow wrote:
>
> Why do people have to add confusion. Because there is no real
> possibility of confusion it has become idiomatic in C++ to refer to a
> reference to const as a 'const reference'. It can only mean one thing,
> no problem of confusion for anyone who is reasonably knowledgeable in
> C++,

It seems that the naming convention of the standard library also played
a great role to add the confusion: const_reference, const_pointer, and
const_iterator, which should really have been reference_to_const,
pointer_to_const, and iterator_to_const. (What a pity!)

I agree that when people use the term 'const reference' it can mean only
one thing, but I think such usage should be discouraged, to keep the
analogy among references, pointers, and iterators. (So, "be strict in
what you say, and lenient in what you accept." :)) Why add the confusion
to just save one word; things are most clearly described when there's no
exception and everything can be described in a uniform way.

> If we stop thinking of references as some kind of hidden pointer we have
> a better chance of not confusing terminology used for speaking/writing
> about references with that used for pointers.

Even without thinking of references as some kind of hidden pointers (in
implementation details), we already have the analogy and similarities
among references, pointers, and iterators on a higher level, and I don't
see why that adds the confusion. What makes references distinct in this
problem is just that they are not objects and they cannot be const.

--
Seungbeom Kim

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

From: Dilip on
Francis Glassborow wrote:
> As for the way we write pointers to const, there are good reasons for
> preferring:
>
> int const * ptr;
>
> to
>
> const int * ptr;
>
> even though they have identical meanings. I first came across a
> carefully worded rationale for this choice from Dan Saks more than a
> decade ago. In simple terms it is far too easy to confuse the later with
>
> int * const cptr;

In C++ Templates, Vandevoorde and Josuttis clearly demonstrate the
exact point you are making. I can't tell if this will cover all
scenarios where const is used in a declaration but for something like:

int const* ptr;

the token preceding the const is always constant. By that logic, its
easy to see that as opposed to the case mentioned above, the following
declares a constant pointer and non-constant data:

int* const ptr;

I can't emphasize how easier it was for me to remember this once I
latched on to this simple rule.


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

From: Francis Glassborow on
In article <eidi8s$f11$1(a)news.Stanford.EDU>, Seungbeom Kim
<musiphil(a)bawi.org> writes
>Even without thinking of references as some kind of hidden pointers (in
>implementation details), we already have the analogy and similarities
>among references, pointers, and iterators on a higher level, and I don't
>see why that adds the confusion. What makes references distinct in this
>problem is just that they are not objects and they cannot be const.


No, what makes references different is that they are not iterators :-)
Pointers are iterators though sometimes only trivial ones. Iterators are
objects, references are not. We do need to get that distinction across
from early on. One of the places where the Standard gets it right is in
distinguishing between a variable and a reference. And, yes, I agree
with you that the const_iterator was unhelpful.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects


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

From: Daveed on

Seungbeom Kim wrote:
> Francis Glassborow wrote:
> >
> > Why do people have to add confusion. Because there is no real
> > possibility of confusion it has become idiomatic in C++ to refer to a
> > reference to const as a 'const reference'. It can only mean one thing,
> > no problem of confusion for anyone who is reasonably knowledgeable in
> > C++,
>
> It seems that the naming convention of the standard library also played
> a great role to add the confusion: const_reference, const_pointer, and
> const_iterator, which should really have been reference_to_const,
> pointer_to_const, and iterator_to_const. (What a pity!)
>
> I agree that when people use the term 'const reference' it can mean only
> one thing, but I think such usage should be discouraged, to keep the
> analogy among references, pointers, and iterators. (So, "be strict in
> what you say, and lenient in what you accept." :)) Why add the confusion
> to just save one word; things are most clearly described when there's no
> exception and everything can be described in a uniform way.
>
> > If we stop thinking of references as some kind of hidden pointer we have
> > a better chance of not confusing terminology used for speaking/writing
> > about references with that used for pointers.
>
> Even without thinking of references as some kind of hidden pointers (in
> implementation details), we already have the analogy and similarities
> among references, pointers, and iterators on a higher level, and I don't
> see why that adds the confusion. What makes references distinct in this
> problem is just that they are not objects and they cannot be const.


I wholeheartedly agree with you. Note in addition that even though
reference types cannot be const, const may syntactically appear on top
of a reference type (and it's generally ignored there, but not always
(in
C++0x):
typedef int &RI;
void f1(RI const x) {
x = 3; // Okay.
}
void f2(RI const &x) { // Allowed in C++0x.
x = 3; // Error.
}

(The code is somewhat silly, but situations like these realistically
occur
during template instantiations.)

When talking about code like this, the term "const reference" suddenly
becomes natural, but the unfortunate convention of using the term for
"reference to const" instead then creates an ambiguity.

Daveed


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