Topic: Defect report [basic.life], cannot call base class functions from constructor, was How to do this??? (function pointer to other class)
Author: kanze@gabi-soft.fr
Date: Sat, 12 Jul 2003 08:00:25 GMT Raw View
Richard Smith <richard@ex-parrot.com> wrote in message
news:<Pine.LNX.4.55.0307101456110.6962@sphinx.mythic-beasts.com>...
[I've cross posted to comp.std.c++, because IMHO, this is a standards
problem, probably a defect. I'd set follow-ups as well, but I'm posting
through Google, which doesn't allow it.]
> Nonsense. You can use "this" perfectly happily in a constructor, just
> be careful that (a) you're not using any members that are not fully
> initialised, and (b) if you're calling virtual functions you know
> exactly what you're doing.
In practice, and I think in intent, you are right. However, the
standard makes some pretty stringent restrictions in 3.8. To start
with, it says (in paragraph 1):
The lifetime of an object is a runtime property of the object. The
lifetime of an object of type T begins when:
-- storage with the proper alignment and size for type T is
obtained, and
-- if T is a class type with a non-trivial constructor, the
constructor calls has COMPLETED.
The lifetime of an object of type T ends when:
-- if T is a class type with a non-trivial destructor, the
destructor call STARTS, or
-- the storage which the object occupies is reused or released.
(Emphisis added.) Then when we get down to paragraph 5, it says:
Before the lifetime of an object has started but after the storage
which the object will occupy has been allocated [which sounds to me
like it would include in the constructor, given the text above] or,
after the lifetime of an object has ended and before the storage
which the object occupied is reused or released, any pointer that
refers to the storage location where the object will be or was
located may be used but only in limited ways. [...] If the object
will be or was of a non-POD class type, the program has undefined
behavior if:
[...]
-- the pointer is implicitly converted to a pointer to a base class
type, or [...]
I can't find any exceptions for the this pointer.
Note that calling a non-static function in the base class, or even
constructing the base class in initializer list, involves an implicit
conversion of this to a pointer to the base class. Thus undefined
behavior. I'm sure that this wasn't the intent, but it would seem to be
what this paragraph is saying.
--
James Kanze GABI Software mailto:kanze@gabi-soft.fr
Conseils en informatique orient e objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
------------ And now a word from our sponsor ---------------------
For a secure high performance FTP using SSL/TLS encryption
upgrade to SurgeFTP
---- See http://netwinsite.com/sponsor/sponsor_surgeftp.htm ----
Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: Sat, 12 Jul 2003 16:10:53 +0000 (UTC) Raw View
kanze@gabi-soft.fr writes:
>I've cross posted to comp.std.c++, because IMHO, this is a standards
>problem, probably a defect.
...
>the standard makes some pretty stringent restrictions in 3.8.
...
>I can't find any exceptions for the this pointer.
The issue is not exceptions for the "this" pointer, rather it is
exceptions for objects which are currently undergoing construction or
destruction. Indeed, there are special rules for those, which are
described in section 12.7 [class.cdtor]. The note in section 3.8
[basic.life] does refer to 12.7, and I'm sure the intent was that 12.7
take precedence over 3.8 for such objects. But the normative wording
in 3.8 doesn't say that. So I agree that this is a defect.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]