Topic: this" cannot be used in ctor handlers?
Author: kprateek88@yahoo.com (Prateek R Karandikar)
Date: Tue, 28 Sep 2004 14:18:08 GMT Raw View
function-try-block:
try ctor-initializer[opt] function-body handler-seq
function-body:
compound-statement
function-definition:
decl-specifier-seq[opt] declarator ctor-initializer[opt] function-body
decl-specifier-seq[opt] declarator function-try-block
5.1 [expr.prim] #3
"The keyword this names a pointer to the object for which a nonstatic
member function (9.3.2) is invoked.
The keyword this shall be used only inside a nonstatic *class member
function body (9.3) or in a constructor
mem-initializer* (12.6.2). The type of the expression is a pointer to
the function's class (9.3.2), possibly
with cv-qualifiers on the class type. The expression is an rvalue."
[Emphasis mine].
According to the grammar, the handler-seq is not part of the
function-body. Does this mean that the keyword this cannot be used in
the handlers of a constructor?
-- --
Abstraction is selective ignorance. -Andrew Koenig
-- --
---
[ 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 ]
Author: belvis@pacbell.net (Bob Bell)
Date: Tue, 28 Sep 2004 20:18:38 GMT Raw View
kprateek88@yahoo.com (Prateek R Karandikar) wrote in message news:<607f883e.0409280604.35b89811@posting.google.com>...
> According to the grammar, the handler-seq is not part of the
> function-body. Does this mean that the keyword this cannot be used in
> the handlers of a constructor?
That's always been my understanding. By the time the catch handlers in
the constructor's function try block execute, the object no longer
exists (members and bases have been destroyed), so what would "this"
point to?
Bob
---
[ 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 ]
Author: Alberto Barbati <AlbertoBarbati@libero.it>
Date: Wed, 29 Sep 2004 05:20:56 GMT Raw View
Prateek R Karandikar wrote:
>
> According to the grammar, the handler-seq is not part of the
> function-body. Does this mean that the keyword this cannot be used in
> the handlers of a constructor?
>
Well, of course! In a handler of a constructor the subobjects either
have never been constructed or they have already been destroyed. Thus
there's no longer a valid "this" object to refer to so it's correct to
disallow the use of the this keyword altogether.
Alberto
---
[ 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 ]
Author: Magnus Fromreide <magfr@comp.std.cpp.magfr.user.lysator.liu.se>
Date: Wed, 29 Sep 2004 19:01:17 GMT Raw View
Alberto Barbati <AlbertoBarbati@libero.it> writes:
> Prateek R Karandikar wrote:
> > According to the grammar, the handler-seq is not part of the
> > function-body. Does this mean that the keyword this cannot be used in
> > the handlers of a constructor?
> >
> Well, of course! In a handler of a constructor the subobjects either
> have never been constructed or they have already been destroyed. Thus
> there's no longer a valid "this" object to refer to so it's correct to
> disallow the use of the this keyword altogether.
>
Actually it is wrong to say that all resources are freed at that point
as the allocated memory space for the object is still there so it
could be possible to write something along the lines of
foo::foo() { }
foo::foo(int)
try {
throw 1;
} catch(...) {
new(this) foo;
}
Hmmm... is the following valid c++?
foo::foo(unsigned)
try {
throw (void*)this;
} catch(void* x) {
new(x) foo;
}
Of course none of the examples are very useful but still.
---
[ 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 ]
Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Wed, 29 Sep 2004 22:18:49 GMT Raw View
Magnus Fromreide wrote:
> Alberto Barbati <AlbertoBarbati@libero.it> writes:
>
>
>>Prateek R Karandikar wrote:
>>
>>>According to the grammar, the handler-seq is not part of the
>>>function-body. Does this mean that the keyword this cannot be used in
>>>the handlers of a constructor?
>>>
>>
>>Well, of course! In a handler of a constructor the subobjects either
>>have never been constructed or they have already been destroyed. Thus
>>there's no longer a valid "this" object to refer to so it's correct to
>>disallow the use of the this keyword altogether.
>>
>
>
> Actually it is wrong to say that all resources are freed at that point
I never said that. I just wrote about subobjects and their state. That
is 100% true. I said nothing about "resources" or "freeing".
> as the allocated memory space for the object is still there so it
> could be possible to write something along the lines of
> <snip>
I agree that the memory is still there, but the object lifetime never
started, so accessing that memory falls under the restrictions of 3.8
(para 5, 6 and 7).
Allowing the use of "this" in the handler would make it too easy to
*involuntary* access such memory in ways that violate 3.8/5, thus
incurring in undefined behaviour. And I can't think of any reasable use
case that requires deliberate access to that memory... ;-)
Alberto
---
[ 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 ]
Author: Michiel.Salters@logicacmg.com (Michiel Salters)
Date: Sat, 2 Oct 2004 04:55:13 GMT Raw View
Magnus Fromreide <magfr@comp.std.cpp.magfr.user.lysator.liu.se> wrote in message news:<871xgleycs.fsf@comp.std.cpp.magfr.user.lysator.liu.se>...
> Alberto Barbati <AlbertoBarbati@libero.it> writes:
> Actually it is wrong to say that all resources are freed at
[that point = ctor try-block ]
> as the allocated memory space for the object is still there so it
> could be possible to write something along the lines of
>
> foo::foo() { }
>
> foo::foo(int)
> try {
> throw 1;
> } catch(...) {
> new(this) foo;
> }
I'd say you missed the fact that the handler has an implicit
throw; at the closing }, similar to the implicit return; in
void functions
Regards,
Michiel Salters
---
[ 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 ]