Binding C++ Value Type (Templates)

Benjamin Thaut via Digitalmars-d digitalmars-d at puremagic.com
Fri Feb 13 05:18:26 PST 2015


There are currently two problems with binding c++ value types and 
c++ value type templates.

1. Semantics are somewhat different. Copying / constructor / 
destructing / operator overloading is a issue.
2. Only templates which are instantiated on the c++ side can be 
used.

Now it would be nice if I could tell a type, that it should be 
mangled like a C++ type but only if it is used in a C++ function 
signature. The entire type would still be implemented on the D 
side and the implementor would have to garantuee that the binary 
layout is the same as on the C++ side.

e.g.

extern(C++, wrapper)
struct SomeVector //mangling exactly like any other D type
{
   this(this)
   {
     D-Style copy code
   }

   void opBinary(string op)(ref SomeVector rh) if(op == "+")
   {
     D-Style operator overloading
   }
}

// In extern C++ decalrations SomeVector is mangled as C++ type.
extern(C++) SomeFunction(SomeVector vec);

That way you could mirror the implementation of Value Types on 
the D side using all D features available. Only the binary layout 
and size of the data members would have to be the same. But it 
would allow to pass value types between D and C++ and use them on 
both sides without the need to write wrapper functions which 
convert from a C++ value type to a D value type.

What do you think?

Kind Regards
Benjamin Thaut


More information about the Digitalmars-d mailing list