Thread overview
Interfacing to C++: Cannot access value from namespace
Sep 07, 2019
Andrew Edwards
Sep 07, 2019
Ali Çehreli
Sep 07, 2019
Andrew Edwards
Sep 07, 2019
Andrew Edwards
Sep 07, 2019
Ali Çehreli
Sep 07, 2019
Andrew Edwards
September 07, 2019
I'm running into the following issue when attempting to interface with C++:

// C++
========
namespace MySpace
{
    MyType& GetData();
}

struct MyType
{
    // ...
    float continuallyUpdatedValue;
    // ...
};

// call site
MySpace::GetData().continuallyUpdatedValue;

// D
========
extern (C++, MySpace) MyType* GetData();

struct MyType
{
    float continuallyChangingValue; // [1]
}

// call site
MySpace.GetData().continuallyUpdatedValue; // <--- This is not being updated by C++

[1] I did not declare any of the other member variables from the struct, don't know if this is a source of the problem.
September 07, 2019
On 09/07/2019 03:26 AM, Andrew Edwards wrote:

>      float continuallyUpdatedValue;
>      float continuallyChangingValue; // [1]

They mean the same thing for an English speaker but compilers don't know that (yet?). :)

Ali

September 07, 2019
On Saturday, 7 September 2019 at 12:24:49 UTC, Ali Çehreli wrote:
> On 09/07/2019 03:26 AM, Andrew Edwards wrote:
>
> >      float continuallyUpdatedValue;
> >      float continuallyChangingValue; // [1]
>
> They mean the same thing for an English speaker but compilers don't know that (yet?). :)
>
> Ali

LOL. Sorry about that, I decided to change the variable name in the middle of writing the post and missed one.

Andrew
September 07, 2019
On Saturday, 7 September 2019 at 12:30:53 UTC, Andrew Edwards wrote:
> On Saturday, 7 September 2019 at 12:24:49 UTC, Ali Çehreli wrote:
>> On 09/07/2019 03:26 AM, Andrew Edwards wrote:
>>
>> >      float continuallyUpdatedValue;
>> >      float continuallyChangingValue; // [1]
>>
>> They mean the same thing for an English speaker but compilers don't know that (yet?). :)
>>
>> Ali
>
> LOL. Sorry about that, I decided to change the variable name in the middle of writing the post and missed one.
>
> Andrew

The problem existed prior to this naming mishap and remains after correcting it.
September 07, 2019
On 09/07/2019 03:26 AM, Andrew Edwards wrote:

> [1] I did not declare any of the other member variables from the struct,
> don't know if this is a source of the problem.

Yes, it definitely is a problem. The members are accessed as byte offsets into their objects. Without defining the other members, your D-side member is read from offset 0, which is not the case on the C++ side.

However, I'm still not sure whether that would solve the problem due to the following differences between C++ and D:

- No 'volatile' keyword in D (not in your example code anyway)

- Variables are thread-local by default in D

But I guess extern(C++) takes care of those differences. (?) So, it should work after defining all the members. :)

Ali

September 07, 2019
On Saturday, 7 September 2019 at 12:39:25 UTC, Ali Çehreli wrote:
> On 09/07/2019 03:26 AM, Andrew Edwards wrote:
>
> > [1] I did not declare any of the other member variables from
> the struct,
> > don't know if this is a source of the problem.
>
> Yes, it definitely is a problem. The members are accessed as byte offsets into their objects. Without defining the other members, your D-side member is read from offset 0, which is not the case on the C++ side.
>
> However, I'm still not sure whether that would solve the problem due to the following differences between C++ and D:

That was it, and yes it solves the problem. There are far too many variables to define at the moment so I found the offsetof() and defined a "void[offset] pad" to take care of the issue for right now.

Thanks,
Andrew