digitalmars.D.learn - Suicidal objects
- Mike (13/13) Dec 09 2007 Out of curiosity I decided to try out how objects could delete themselve...
- Jarrett Billingsley (9/19) Dec 10 2007 There _is_ actually placement new in D, it just isn't provided by defaul...
- Sean Kelly (8/34) Dec 10 2007 This thread may be relevant:
-
Jarrett Billingsley
(5/9)
Dec 10 2007
"Sean Kelly"
wrote in message - Sean Kelly (6/17) Dec 10 2007 Yeah, but it would still be a bit awkward to use __traits to obtain the
- Jarrett Billingsley (28/46) Dec 10 2007 template Sizeof(T)
- Jarrett Billingsley (6/13) Dec 10 2007 Although I will agree that your solution is much more elegant and
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
"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
Jarrett Billingsley wrote:"Mike" <vertex gmx.at> wrote in message news:op.t224pyqokgfkbn lucia...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. SeanOut 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
"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.htmlIn 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
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.htmlYeah, 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. SeanIn 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
"Sean Kelly" <sean f4.ca> wrote in message news:fjk58r$va5$1 digitalmars.com...Jarrett Billingsley wrote: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.."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.htmlYeah, 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. SeanIn 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
"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