Using C++ with D / returning a templated type from C++

Robert M. Münch robert.muench at saphirion.com
Thu Jul 5 06:35:01 UTC 2018


On 2018-07-04 18:04:25 +0000, Steven Schveighoffer said:

> On 7/4/18 1:32 PM, Robert M. Münch wrote:
>> I have the following C++ code and want to give the D/C++ integration a new try:
>> 
>>     template<typename T> class Array {...}
>>     class myClass {...}
>>     typedef Array<myClass> myClassArray;
>>     myClassArray classA::getArray() noexcept {...}
>> 
>> How does the D binding for this look like? I tried something like this:
>>  extern (C++) {
> 
> You need class myClass somewhere, no?

Yes, it's the input to an other function:

    extern (C++, myUtils) {
      final int readFromFile(Destination dst, Array!myClass input, 
const char* filename);
    }

>>    class Array(T){};
>>    class Array(myClass) {};
> 
> Not sure what this is? ^^

Beside the wrong syntax, I was not sure if I need to instantiate the 
specific template.

> Note, I would say you need: alias myClassArray = Array!myClass

Yes, might help.

>>    class classA {
>>      final Array(myClass) getArray(); <== COMPILER ERRORS
> 
> Array!myClass, not Array(myClass)

Ah... this is always catching me... because the declaration syntax is 
different. Thanks, helped a bit.

This is the C++ signature: public: static class Array<class myClass> 
__cdecl builtinCodecs(void)
And this is now the D version: public: class Array<class myClass> 
__cdecl builtinCodecs(void) __ptr64

So, the only difference left is the C++ static and the additional 
__ptr64 (whatever this is) on the D side. Any further ideas?

BTW: IMO the docs should show very complex examples taking classes, 
typedefs, templates etc. into account and not only show for simple 
basic types. That's OK to get the concept but this C++ interface thing 
needs a ton of examples how to do things to be really useable. So, how 
or where could such a collection be done?


-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20180705/ff739512/attachment.html>


More information about the Digitalmars-d-learn mailing list