From -7387319207580257387
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,fe6310a7fb6a4c8c
X-Google-Attributes: gidf78e5,public
From: clamage@Eng.Sun.COM (Steve Clamage)
Subject: Re: const int& or int const&
Date: 1998/02/10
Message-ID: <6bigu4$9kv@engnews1.Eng.Sun.COM>#1/1
X-Deja-AN: 323909439
References: <34D8AD3C.5C38416B@heidelbg.ibm.com> <34D9D6B5.30EE@central.beasys.com> <34DBE467.521EA970@vizdom.com>
X-Original-Date: 7 Feb 1998 20:41:40 GMT
Originator: austern@isolde.mti.sgi.com
Organization: Sun Microsystems Inc., Mountain View, CA
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBNODiBky4NqrwXLNJAQEGUwH7BRjMKrc+vw/yz2knbOfOmwXnPWB7aVQQ hym+GvtHo44n4xm0kCI3HIKS6DiSSnhEbfPqLffAo5Sjy7XzoKFGwQ== =dmOn
Newsgroups: comp.std.c++


John Lacey <johnl@vizdom.com> writes:

>David R Tribble wrote:
>> 
>> There seems to be a minority of programmers who insist on
>> placing the 'const' specifier of a type after the type, such
>> as 'int const' instead of 'const int'.  Personally I abhor this
>> style; it's just another way to confuse people and is a break from
>> well-accepted practice that's been around for several years now.

>The entire declaration syntax is regrettable and confusing. 

Yes. It is inherited from C, and given the goals of C++, could
not reasonably be changed. Too bad, because the syntax is
not only difficult for people to read and write, but is
difficult for compilers to parse. (The declaration syntax is
not LR(k), for grammar afficionados.)

>The reason for the behavior of the minority, as I'm sure David
>knows, is that "const" is a postfix qualifier, except when it
>appears in the left-most position. I agree that the combination
>is a source of confusion, but I believe the left-most exception
>is the problem, not the solution. 

Actually, it is neither a prefix nor a postfix modifier. It
is part of a "declaration-specifier". A declaration-specifier
modifies the "declarator" which follows. (Except for the storage
class, which applies to the top-level declarator, not to any
intermediate declarator. Sigh.)

The point of confusion occurs when people try to simplify
the complicated syntax rules. The simplifications ("modifies what
comes before", "modifies what comes after", "look right then left",
"look inside out", etc) are not always correct and helpful.

A declaration-specifier may include storage class, type name,
and const or volatile qualifiers (and some things we'll ignore).
Example:
	extern const int foo;
The first three words are all part of one declaration-specifier,
and their order doesn't matter. Any of the six permutations of the
three words has the same meaning.  The declarator in this example
is "foo", and therefore represents a constant int object with
external linkage.

A pointer or reference operator is itself (part of) a declarator,
not a declaration-specifier.
Example:
	int const * volatile x;
Here, the first declaration-specifier is "int const", and modifes
the pointer. The next declaration-specifier is "volatile" and
modifies the declator x. So x is volatile, and is a pointer. It
points to a constant int.

If you just thought, "Aha! I just have to read right to left!",
you fell into a common trap. That rule doesn't work for function
declarations, among other things. But that's enough for now.

---
Steve Clamage, stephen.clamage@sun.com

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern/std-c++/faq.html                  ]



