Suicidal objects
Jarrett Billingsley
kb3ctd2 at yahoo.com
Mon Dec 10 12:28:39 PST 2007
"Sean Kelly" <sean at f4.ca> wrote in message
news:fjk58r$va5$1 at digitalmars.com...
> Jarrett Billingsley wrote:
>> "Sean Kelly" <sean at f4.ca> wrote in message
>> news:fjjpgq$2t9c$3 at 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..
More information about the Digitalmars-d-learn
mailing list