Re: Problem with typedef'ed pointer used as const return type
Greg Herlihy wrote:
On Jan 8, 7:37 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
Here your function returns a pointer to a const Foo. The proper way to
declare your function would be
Foo const * func1()
(where 'const' follows the type it qualifies).
"Foo const *" is a properly confusing way to specify a pointer to a
const Foo (though, admittedly, it's a great way to specify to a
pointer to a Foo const). After all, how on earth can sandwiching the
"const" qualifier between two potential targets (Foo) and (*) be
better than having "const" unambiguously refer to "Foo" by appearing
right in front of it?
And if there is something improper about a "const Foo" declaration,
then why do all the Standard Library interfaces and examples in the C+
+ Standard invariably place a "const" before the target type, and not
afterwards?
Sorry. Misuse of the word "proper". Ought to say "better". "A better
way to declare your function would be..."
The rule of the language says that a declaration of a pointer is
T * cv-qual-seq(opt) D
where T is a type, and D is the identifier. T can include another set
of cv-qual as well. So, it does not matter *to the compiler* whether a
pointer to a const Foo is declared as
const Foo * blah
or
Foo const * blah
There is nothing improper of writing 'const Foo' of course.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask