www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Suicidal objects

reply Mike <vertex gmx.at> writes:
Out of curiosity I decided to try out how objects could delete themselves  
and what happened if they did. I found out that "delete this;" actually  
works.

AFAIK there's no placement new in D, which could be a reason for allowing  
this behavior. Other than that, any attempts to delete or assign to "this"  
should be illegal inside methods. It's a minor thing - I don't think  
"delete this;" is a common typo and any sane programmer wouldn't do that  
on purpose.

So: what do you think? Are there any useful applications for  
assigning/deleting "this"?

-Mike

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Dec 09 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Mike" <vertex gmx.at> wrote in message news:op.t224pyqokgfkbn lucia...
 Out of curiosity I decided to try out how objects could delete themselves 
 and what happened if they did. I found out that "delete this;" actually 
 works.

 AFAIK there's no placement new in D, which could be a reason for allowing 
 this behavior. Other than that, any attempts to delete or assign to "this" 
 should be illegal inside methods. It's a minor thing - I don't think 
 "delete this;" is a common typo and any sane programmer wouldn't do that 
 on purpose.

 So: what do you think? Are there any useful applications for 
 assigning/deleting "this"?
There _is_ actually placement new in D, it just isn't provided by default like it is in C++. You can overload the 'new' operator on a per-class basis, and writing a placement new is as easy as returning the pointer passed into 'new'. Furthermore, scoped class references allocated with 'new' like: scope a = new A(); are actually allocated on the stack. In both cases, 'delete this' seems bogus..
Dec 10 2007
parent reply Sean Kelly <sean f4.ca> writes:
Jarrett Billingsley wrote:
 "Mike" <vertex gmx.at> wrote in message news:op.t224pyqokgfkbn lucia...
 Out of curiosity I decided to try out how objects could delete themselves 
 and what happened if they did. I found out that "delete this;" actually 
 works.

 AFAIK there's no placement new in D, which could be a reason for allowing 
 this behavior. Other than that, any attempts to delete or assign to "this" 
 should be illegal inside methods. It's a minor thing - I don't think 
 "delete this;" is a common typo and any sane programmer wouldn't do that 
 on purpose.

 So: what do you think? Are there any useful applications for 
 assigning/deleting "this"?
There _is_ actually placement new in D, it just isn't provided by default like it is in C++. You can overload the 'new' operator on a per-class basis, and writing a placement new is as easy as returning the pointer passed into 'new'. Furthermore, scoped class references allocated with 'new' like: scope a = new A(); are actually allocated on the stack. In both cases, 'delete this' seems bogus..
This thread may be relevant: http://www.digitalmars.com/d/archives/digitalmars/D/Inheriting_constructors_54088.html In short, there's no easy way to perform placement new on an existing class, and I view this as a deficiency. My proposal would provide one easy way to do so, but I still feel that making it automatic would be preferable. Sean
Dec 10 2007
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message 
news:fjjpgq$2t9c$3 digitalmars.com...

http://www.digitalmars.com/d/archives/digitalmars/D/Inheriting_constructors_54088.html
 In short, there's no easy way to perform placement new on an existing 
 class, and I view this as a deficiency.  My proposal would provide one 
 easy way to do so, but I still feel that making it automatic would be 
 preferable.
With __traits, we can now get the size of a class instance and the overloads of the constructor (I think?). I think that's about all you'd need, no?
Dec 10 2007
parent reply Sean Kelly <sean f4.ca> writes:
Jarrett Billingsley wrote:
 "Sean Kelly" <sean f4.ca> wrote in message 
 news:fjjpgq$2t9c$3 digitalmars.com...
 
 http://www.digitalmars.com/d/archives/digitalmars/D/Inheriting_constructors_54088.html
 In short, there's no easy way to perform placement new on an existing 
 class, and I view this as a deficiency.  My proposal would provide one 
 easy way to do so, but I still feel that making it automatic would be 
 preferable.
With __traits, we can now get the size of a class instance and the overloads of the constructor (I think?). I think that's about all you'd need, no?
Yeah, but it would still be a bit awkward to use __traits to obtain the size, allocate, then use __traits to call the proper ctor by name on the new block or whatever. Placement new is so generally useful that it should really just be supported by the language. Sean
Dec 10 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message 
news:fjk58r$va5$1 digitalmars.com...
 Jarrett Billingsley wrote:
 "Sean Kelly" <sean f4.ca> wrote in message 
 news:fjjpgq$2t9c$3 digitalmars.com...

 http://www.digitalmars.com/d/archives/digitalmars/D/Inheriting_constructors_54088.html
 In short, there's no easy way to perform placement new on an existing 
 class, and I view this as a deficiency.  My proposal would provide one 
 easy way to do so, but I still feel that making it automatic would be 
 preferable.
With __traits, we can now get the size of a class instance and the overloads of the constructor (I think?). I think that's about all you'd need, no?
Yeah, but it would still be a bit awkward to use __traits to obtain the size, allocate, then use __traits to call the proper ctor by name on the new block or whatever. Placement new is so generally useful that it should really just be supported by the language. Sean
template Sizeof(T) { if(is(T == class)) const Sizeof = __traits(getInstanceSize, T); else const Sizeof = T.sizeof; } template CtorForwards(T) { // use __traits to get ctor overloads, generate them // maybe use strings and then string mixin? probably. } class Placement(T) : T { new(size_t s, void* p) { return p; } mixin(CtorForwards!(T)); } .. ubyte[Sizeof!(A)] place; auto a = new(place.ptr) Placement!(A)(params); This is kind of what Walter meant by __traits not having to be pretty to be useful; it can be hidden behind templates. Of course, if D were perfect, this kind of stuff wouldn't be crufty-looking, but nothing's ever perfect..
Dec 10 2007
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message 
news:fjjpgq$2t9c$3 digitalmars.com...
 Jarrett Billingsley wrote:

 This thread may be relevant:

 http://www.digitalmars.com/d/archives/digitalmars/D/Inheriting_constructors_54088.html

 In short, there's no easy way to perform placement new on an existing 
 class, and I view this as a deficiency.  My proposal would provide one 
 easy way to do so, but I still feel that making it automatic would be 
 preferable.
Although I will agree that your solution is much more elegant and straightforward. Tiny modifications that make lots of things possible are cool. Requiring complex metaprogramming to work around deficiencies in the language, not so much.
Dec 10 2007