C++ overloaded operators and D

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Tue Nov 11 18:37:51 PST 2014


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 ?


More information about the Digitalmars-d mailing list