C++ constructors, destructors and operator access

nazriel spam at dzfl.pl
Mon May 20 13:11:25 PDT 2013


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?

Isn't it possible already with something like:

extern(C++) interface Foo
{
     pragma(mangle, typeof(this).mangleof ~ 
generateCPPMangle!"myOwnFunctionMangling") void foo();
}

AFAIK, mangle pragma was merged recently.


More information about the Digitalmars-d mailing list