On the richness of C++

Sean Kelly sean at invisibleduck.org
Thu Apr 10 16:26:33 PDT 2008


== Quote from Kevin Bealer (kevinbealer at gmail.com)'s article
> If you want to do really specific things with memory, C++ lets you do this, but
> in D classes can only be handled by their object references.  You can't do a
> "placement new" or anything like it with D classes.  In C++ you could build
> classes that automatically allocate themself in a shared memory segment, but
> in D, probably not.  Note that in practice, this is hard to do right and I think is
> rarely used for much.

I'd personally like for "placement new" to be supported by the language.  I've
considered adding a new(void*) method to Object for this purpose in Tango,
but haven't experimented enough to find out if this would cause any problems.
One I can think of is that certain hierarchies seem like they could cause this
to throw:

    class Object {
        void* new( size_t s, void* p ) {
            return p;
        }
    }

    class C {
        void* new( size_t s, Allocator a ) {
            return a.alloc( s );
        }
    }

    class D {
        void* new( size_t s, Allocator a ) {
            printf( "hello!\n" );
            return super.new( s, a );
    }

    void* pos = ...;
    auto  var = new( pos ) D;

With the vtbl checking in D, I think the above call will throw a "not implemented"
message because D overrides one inherited "new" routine but not the "placement
new" routine inherited from Object.  In fact, this has me wondering about the
practicality of this sort of checking at all.  It seems a fairly straightforward
design for the language to consider broken.


Sean



More information about the Digitalmars-d mailing list