Small structs: interfacing with C++ potentially broken

Jan Jan at Krassnigg.de
Mon Dec 20 10:24:00 UTC 2021


I have a small struct that I'm trying to interface to.

C++
```cpp
struct __declspec(dllexport) SmallStruct
{
   float value = 0;
   //float value2 = 0;
   //float value3 = 0;

   SmallStruct(float val)
     : value(val)
   {
   }

   static SmallStruct GetValue(float input)
   {
     return SmallStruct(input * 3.141f);
   }
};
```

And on the D side:

```cpp
extern(C++) struct SmallStruct
{
   float value = 0;
//   float value2 = 0;
//   float value3 = 0;

   static SmallStruct GetValue(float input);
};
```

Then I use it like this:

```cpp
SmallStruct s = SmallStruct.GetValue(3);
```

Running this crashes on the C++ side, with an access violation 
writing data.
Looking at the disassembly, it seems that C++ expects the 
Smallstruct return object to be passed in, whereas D assumes that 
the SmallStruct is passed through registers.

This is with MVSC 2019 compiled for x64 and DMD v2.098.0.

If I enable 'value2' and 'value3', it seems that both compilers 
start to agree on the calling convention.

Is this a known issue, or is there a way to instruct DMD to use a 
specific calling convention for a given type?



More information about the Digitalmars-d-learn mailing list