On Saturday, 18 May 2013 at 22:23:51 UTC, Igor Stepanov wrote:
At the current time D have powerful mechanism of access to C++ classes. For access to methods of C++ classes (virtual and not) we can use extern(C++) interface.

//С++

class CPPTest1
{
    int a;
    int b;
  public:
    virtual int boom();
    int fun();
    static int gun();
    CPPTest1(int);
    virtual ~CPPTest1();
    int& operator[](size_t);
};

class CPPTest2: public CPPTest1
{
    int boom();
};

//D
extern(C++)interface CPPTest1
{
    int boom();
    static int gun();
    final int fun();
}

extern(C++)interface CPPTest2: CPPTest1
{
    //int boom();
}



As a rule, non-static fields are not public in C++ classes and is not part of interface. Thus the most of C++ classes can be bound without any glue c++ code. However D dont support C++ overloaded operators and constructors. Yes, we cannot make mapping C++ operators to D operators and C++ constructors to D constructors). Nonetheless С++ operators and constructors are the simple C++ functions or methods with special mangling. Thus I've suggest next mechanism: Allow special pragma(cppSymbol, string_arg), when string_arg is the name of c++ thing.
Example:

extern(C++)interface CPPTest1
{
    int boom();
    static int gun();
    final int fun();
    ///!!!!
pragma(cppSymbol, "constructor") final void ctor(int); //linked with CPPTest1(int); pragma(cppSymbol, "destructor") void dtor(); //linked with virtual ~CPPTest1(); pragma(cppSymbol, "[]") ref int indexOf(size_t); //linked with int& operator[](size_t);
}

This pragma must apply to the function (or method), use natural C++ mangle, but set operatror or constructor or destructor mangled name instead of function name.

Is it useful idea?

you can do this yourself just with mixins and templates, but the real problem is c++ abi and name mangling. so you will need to do runtime search for c++ mangled names and which runtime used, get it and call placement new and ctor(with the help of asm{} of course), do the same for other operators. it is possible, but this is never be safe at least due to non-standardized name mangling.

you can write small library which would help doing this all if you really want this.

Reply via email to