C++ constructors, destructors and operator access

Igor Stepanov wazar.leollone at yahoo.com
Mon May 20 13:24:08 PDT 2013


On Monday, 20 May 2013 at 20:13:47 UTC, nazriel wrote:
> On Monday, 20 May 2013 at 20:11:27 UTC, nazriel wrote:
>> 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();
>> }
>>
> Of course I mean:
>
> extern(C++) interface Foo
> {
>     pragma(mangle, generateCPPMangle!(typeof(this).mangleof, 
> "myOwnFunctionMangling")) void foo();
> }
>
>> AFAIK, mangle pragma was merged recently.

If we'll implement generateCPPMangle template, we'll can do all:)
This way one of the possible. There are another way to do it. I 
don't know, what a best way now. But I think, this (simple 
binding C++ code to D) is important issue and we should solve it 
using one of the possible way.


More information about the Digitalmars-d mailing list