Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
December 09, 2007 Suicidal objects | ||||
---|---|---|---|---|
| ||||
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/
|
December 10, 2007 Re: Suicidal objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike | "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.. |
December 10, 2007 Re: Suicidal objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 |
December 10, 2007 Re: Suicidal objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | "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? |
December 10, 2007 Re: Suicidal objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | "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. |
December 10, 2007 Re: Suicidal objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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
|
December 10, 2007 Re: Suicidal objects | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | "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.. |
Copyright © 1999-2021 by the D Language Foundation