www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I call a C++ struct default constructor from D?

reply Atila Neves <atila.neves gmail.com> writes:
I can declare a C++ struct like so:

extern(C++, mynamespace)
struct Foo {
    //...
}

But... I don't want to repeat the initialisation code for that 
struct's default constructor. I can't declare one in D because D 
doesn't allow default constructors for structs. What's my way 
out? Thanks,

Atila
Feb 07
parent reply kinke <noone nowhere.com> writes:
On Tuesday, 7 February 2017 at 10:15:09 UTC, Atila Neves wrote:
 I can declare a C++ struct like so:

 extern(C++, mynamespace)
 struct Foo {
    //...
 }

 But... I don't want to repeat the initialisation code for that 
 struct's default constructor. I can't declare one in D because 
 D doesn't allow default constructors for structs. What's my way 
 out? Thanks,

 Atila
I'm afraid there's no way out. I summarized some of my C++ interop findings incl. default constructor here: http://forum.dlang.org/thread/nqxsdehlydizatoprrax forum.dlang.org
Feb 07
parent reply Atila Neves <atila.neves gmail.com> writes:
On Tuesday, 7 February 2017 at 10:46:24 UTC, kinke wrote:
 On Tuesday, 7 February 2017 at 10:15:09 UTC, Atila Neves wrote:
 I can declare a C++ struct like so:

 extern(C++, mynamespace)
 struct Foo {
    //...
 }

 But... I don't want to repeat the initialisation code for that 
 struct's default constructor. I can't declare one in D because 
 D doesn't allow default constructors for structs. What's my 
 way out? Thanks,

 Atila
I'm afraid there's no way out. I summarized some of my C++ interop findings incl. default constructor here: http://forum.dlang.org/thread/nqxsdehlydizatoprrax forum.dlang.org
Ugh, I was afraid of that. I ended up having to write (!) a C++ function that returned the default-initialised struct and called that from D. It got uglier soon after... I've only every done trivial C++ integration before. As soon as I tried something "real" it all broke down incredibly fast. Probably going to have to file some bugs on name mangling. Atila
Feb 07
next sibling parent MGW <mgw yandex.ru> writes:
On Tuesday, 7 February 2017 at 13:37:01 UTC, Atila Neves wrote:
 On Tuesday, 7 February 2017 at 10:46:24 UTC, kinke wrote:

 I've only every done trivial C++ integration before. As soon as 
 I tried something "real" it all broke down incredibly fast. 
 Probably going to have to file some bugs on name mangling.


 Atila
Perhaps this video will help https://www.youtube.com/watch?v=HTgJaRRfLPk
Feb 07
prev sibling parent reply MGW <mgw yandex.ru> writes:
On Tuesday, 7 February 2017 at 13:37:01 UTC, Atila Neves wrote:

Here still example
https://pp.vk.me/c636630/v636630885/46579/neSdIip1ySI.jpg
Feb 07
parent evilrat <evilrat666 gmail.com> writes:
On Tuesday, 7 February 2017 at 14:26:11 UTC, MGW wrote:
 On Tuesday, 7 February 2017 at 13:37:01 UTC, Atila Neves wrote:

 Here still example
 https://pp.vk.me/c636630/v636630885/46579/neSdIip1ySI.jpg
I'm sorry for being offensive, but... Of course, and the next step will be pragma mangle on D function to map it to correct ctor, then we call malloc(oops, how do we know correct class instance size?), then we call placement new from C++ RT on malloc'd memory, easy. Oh and then we also should call pragma mangled dtor, we do want no mem leaks, right? Now, sorry for being sarcastic but this is not the way, just imagine making all this new wrappers for every ctor manually for any serious project like Qt, PhysX, Bullet Physics, or FBX or Alembic SDK... Ok, and then we are standing at the point of supporting the future releases and changes
Feb 08