www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using C++ with D / returning a templated type from C++

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
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++) {
    class Array(T){};
    class Array(myClass) {};
    class classA {
      final Array(myClass) getArray(); <== COMPILER ERRORS
    };
  }
But this doesn't work at all and give a bunch of comiler errors:
Error: function declaration without return type. (Note that 
constructors are always named this)
Error: no identifier for declarator extern (C++) Array(myClass)
Error: semicolon expected following function declaration
Error: function declaration without return type. (Note that 
constructors are always named this)
Error: no identifier for declarator extern (C++) getArray()


Any ideas?
-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
Jul 04 2018
next sibling parent vit <vit vit.vit> writes:
On Wednesday, 4 July 2018 at 17:32:49 UTC, 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++) {
    class Array(T){};
    class Array(myClass) {};
    class classA {
      final Array(myClass) getArray(); <== COMPILER ERRORS
    };
  }
 But this doesn't work at all and give a bunch of comiler errors:
 Error: function declaration without return type. (Note that 
 constructors are always named this)
 Error: no identifier for declarator extern (C++) Array(myClass)
 Error: semicolon expected following function declaration
 Error: function declaration without return type. (Note that 
 constructors are always named this)
 Error: no identifier for declarator extern (C++) getArray()


 Any ideas?
final Array!myClass getArray(); <== !!!!!!!!!!!!!!!!!
Jul 04 2018
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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?
     class Array(T){};
     class Array(myClass) {};
Not sure what this is? ^^ Note, I would say you need: alias myClassArray = Array!myClass
     class classA {
       final Array(myClass) getArray(); <== COMPILER ERRORS
Array!myClass, not Array(myClass) -Steve
Jul 04 2018
parent reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
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
Jul 04 2018
parent reply Seb <seb wilzba.ch> writes:
On Thursday, 5 July 2018 at 06:35:01 UTC, Robert M. Münch wrote:
 So, the only difference left is the C++ static and the 
 additional __ptr64 (whatever this is) on the D side. Any 
 further ideas?
Could you post your current C++ and D files?
 So, how or where could such a collection be done?
Well, if you discover something that isn't covered/mentioned on the specification, it can even go to the spec: https://dlang.org/spec/cpp_interface.html Otherwise, I think the C++ interface isn't really mentioned to the DTour, so it could be worthwhile to add a page there and content can be more informal there: https://github.com/dlang-tour/english Otherwise, just plain markdown documents in a GitHub repo are pretty popular these days. Alternatively, there's still the DWiki too. BTW if you (or someone) has a few good examples, I think the DBlog would be more than happy about an article.
Jul 05 2018
parent =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
On 2018-07-05 08:14:18 +0000, Seb said:

 On Thursday, 5 July 2018 at 06:35:01 UTC, Robert M. Münch wrote:
 So, the only difference left is the C++ static and the additional 
 __ptr64 (whatever this is) on the D side. Any further ideas?
Could you post your current C++ and D files?
It's a big code base... that's a bit the problem why I try to strip things down. Maybe this is better: https://pastebin.com/dpQdAPye This is the demangled form from the C++ link library: public: static class b2d::Array<class b2d::ImageCodec> __cdecl b2d::ImageCodec::builtinCodecs(void) With the D compiler I was able to generates these manglings: public: class b2d::Array<class b2d::ImageCodec> * __ptr64 __cdecl b2d::ImageCodec::builtinCodecs(void) __ptr64 public: class b2d::Array<class b2d::ImageCodec> __cdecl b2d::ImageCodec::builtinCodecs(void) __ptr64 -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Jul 05 2018