On Wednesday, 12 November 2014 at 02:37:52 UTC, deadalnix wrote:
On Tuesday, 11 November 2014 at 22:26:48 UTC, IgorStepanov wrote:
Now D provides very powerfull means to link C++ code with D.
However D doesn't allow to call C++ overloaded operators.
It's very annoying, because C++ code may don't provide non-operator analogues. What we know about C++ overloadable operators? Overloaded operator in C++ is a trivial function/method with special name. Thus operator[](int) differs from op_index(int) function only by mangle. C++ OO have a different behaviour from D OO (for example C++ allows different < and > operator overloads or static function fro binary operators), thus we should avoud the temptation of map C++ OOs to D OOs, or back.

Also D provides a pragma(mangle) which allows to redefine symbol mangle. It takes a string argument and redefine mangle to it:
pragma(mangle, "foo") void bar();//bar.mangleof == foo

I suggest to modify pragma(mangle) to support C++ operators.
If argument of this pragma is identifier (for example cppOpAdd), the pragma applied to extern(C++) function or method, compiler mangle the function in accordance with this identifier.

//C++
struct Foo
{
   int& operator[](int);
   //another fields
};

//D
extern(C++) struct Foo
{
   pragma(mangle, cppOpIndex) ref int op_index(int);
   //another fields
}

//using:
Foo f;
f.op_index(1)++; //OK, op_index is linked with Foo::operator[]
f[1]++; //Error, no special behaviour for op_index

I think this approach is simple, doesn't modify the language, can be easily implemented and usefull. Destroy!

Why would you want to go that road ? Souldn't extern(C++) struct mangle this the right way by themselves ?

C++ and D provides different behaviour for operator overloading.
D has a opIndex + opIndexAssign overloads, and if we want to map opIndex to operator[], we must to do something with opIndexAssign. operator< and operator> can't be mapped to D. Same for operator&. Binary arithmetic operators can't be mapped to D, if them implemented as static functions:

Foo operator+(int a, Foo f); //unable to map it to D, because static module-level Foo opAdd(int, Foo) will not provide the same behaviour as operator+ in D. Thus: C++ and D overloaded operators should live in different worlds.

Reply via email to