Re: [guppi-list] foo const & vs. const foo &



> Since we've been discussing style concerning the pros and cons of
> "unsigned", I've got another one for you.  I've noticed that Asger
> (and now Mikkel) write const references and pointers as
> 
>   foo const &
>   foo const *
> 
> whereas everyone else I've ever seen (include Stroustrup and the
> authors of the .h files distributed with egcs) would write the same
> type as
> 
>   const foo &
>   const foo *
> 
> What is the origin of/motivation for this alternative syntax?  Is
> there anything that makes it preferable to the more standard form?

I think it is more consistent.  In C++ (and C), the types are to be
read from right to left:

	char * *

is a pointer to a pointer to a char,

while

	char * & *

is a pointer to a reference to a pointer to a char, and so on.

Similarly,

	char const *

is a pointer to constant chars.

	char const * *

is a pointer to a pointer to constant chars, and

	char * const *

is a pointer to a constant pointer to (modifiable) chars.

So, when you write "const char *", you have to know that the const
part binds strongest and that this is a pointer to constant chars.
In other words, you abandon the rule of reading order, and suddenly
you have to use precedence rules to understand the type.

What about "const char * *"?

And sometimes you end up with stuff like "const char * const *",
which I find harder to read than the equivalent "char const * const *".

This is why I prefer the "string const &" form rather than "const string &".

I know Bjarne Stroustrup writes "const string&", but that's not the
easiest to read, IMO.  However, if you prefer this form, I can switch
in order to have consistent sources.

Greets,

Asger



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]