Hi,

I don't generally disagree, that GCC does the correct thing here. I'm 
completely satisfied if you don't change GCC.
I'm saying that the way I thought about const + ctor seems logical (if you are 
not a compiler developer at least :-) ). Regardless of how well defined 
"return value" is in the standards, to me as C++ developer the ctor acts as a 
function that takes some input and leaves as output a constructed object.

Therefore, please be so nice to non-compiler developers and add a few words 
about ctors (possibly about C++ member functions in general, since they all 
have an implicit pointer argument) to the const attribute docs.

How about this patch to the docs?
- Note that a function that has pointer arguments and examines the data 
pointed to must not be declared const.
+ Note that a function that has pointer arguments and examines the data 
pointed to must not be declared const. This includes all C++ member functions, 
including constructors, which have an implicit this-pointer argument.

On Friday 25 February 2011 17:42:40 Dave Korn wrote:
> On 25/02/2011 15:43, Matthias Kretz wrote:
> > I fully understand why it happened. So I imply your answer is that ctors
> > do not have a return value and my expectation, as explained above, is
> > wrong.
> 
>   You'd already know if ctors had return values, because you'd have had to
> be writing return statements in them all along if they did.  For chapter
> and verse, c++ spec (iso/iec 14882) 12.1.10:
>
> > No return type (not even void) shall be specified for a constructor. A
> > return statement in the body of a constructor shall not specify a return
> > value. [ ... ]
> 
>   No return values there.  (You may have been thinking of overloaded
> operators, that often end with a "return *this" statement.)
> 
> > The website says:
> > "Many functions do not examine any values except their arguments, and
> > have no effects except the return value. Basically this is just slightly
> > more strict class than the pure attribute below, since function is not
> > allowed to read global memory.
> > Note that a function that has pointer arguments and examines the data
> > pointed to must not be declared const. Likewise, a function that calls a
> > non-const function usually must not be const. It does not make sense for
> > a const function to return void."
> > 
> > All of these statements hold for my ctor in question:
> > 1. only examines its arguments
> > 2. no effects other than writing to its class members
> 
>   So, there you go.  The this object in a constructor is not the "return
> value", so writing the members counts as "effects except the return value".
> Basically, making your ctor const told the compiler that it would not write
> to any of the class object's members!
> 
>     cheers,
>       DaveK

Regards,
        Matthias


-- 
Dipl.-Phys. Matthias Kretz
http://compeng.uni-frankfurt.de/index.php?id=mkretz

Reply via email to