|
Boost : |
From: Daryle Walker (darylew_at_[hidden])
Date: 2001-07-24 13:26:34
on 7/24/01 12:40 AM, Tim Butler at tim_at_[hidden] wrote:
[SNIP]
> ------------------------------------------------------------------
>> 6.7. Place const after the base type it modifies. This simple rule makes it
>> easier to write type declarations correctly and will make your declarations
>> consistent with compiler-generated type descriptions in error messages.
>
> o I disagree with this simply because I suspect the "const before" form is
> more familiar to most people. I think most people find the *compiler* messages
> confusing and I don't think it is valuable making them spend extra effort
> interpreting declarations in the code with an unfamiliar style. In fact there
> are a couple instances in this document that appear wrong but require a
> "double take".
I also disagree with the compiler message reason, especially since my
compiler does _not_ do it (i.e. it puts the "const" before the type when
possible).
There is a better reason; putting the qualifier after its base type makes
the placement consistent with all types. I don't think you realize that not
all types support the pre-qualifier form! Pointer declarations, that are
not hidden in a typedef, must use the post-qualifier form. However, all
pre-qualifier types can be post-qualified, so using post-qualification all
the time is the most consistent.
const int i;
A constant integer
int const i;
Also a constant integer
const int * p;
A regular (i.e. non-const) pointer to a constant integer
int const * p;
Also a regular pointer to a constant integer
int * const p;
A constant pointer to a regular integer
const int ** p;
A regular pointer to a regular pointer to a constant integer
int const ** p;
Also a regular pointer to a regular pointer to a constant integer
int * const * p;
A regular pointer to a constant pointer to a regular integer
int ** const p;
A constant pointer to a regular pointer to a regular integer
volatile int const * volatile * const * p;
A regular pointer to a constant pointer to a volatile
pointer to a constant & volatile integer
> You might rephrase this:
>
> Place const before the base type it modifies. Every C++ textbook does this and
> so does the C++ standard. :) (please note smiley)
Use the reason I just gave instead.
> Also, when I see a
> const int = 42;
> its easier to imagine the 'const' filling a position for 'static'.
[TRUNCATE]
-- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT mac DOT com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk