Thread overview
How do I call a C++ struct default constructor from D?
Feb 07, 2017
Atila Neves
Feb 07, 2017
kinke
Feb 07, 2017
Atila Neves
Feb 07, 2017
MGW
Feb 07, 2017
MGW
Feb 08, 2017
evilrat
February 07, 2017
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
February 07, 2017
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
February 07, 2017
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


February 07, 2017
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

February 07, 2017
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

February 08, 2017
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