How to pass a class by (const) reference to C++

Tim tim.dlang at t-online.de
Thu Dec 16 16:21:30 UTC 2021


On Thursday, 16 December 2021 at 08:30:14 UTC, Jan wrote:
> Ok, next problem. Maybe you know the necessary syntax for this 
> one too.
>
>
> C++
> ```cpp
> class Test
> {
> public:
>   __declspec(dllexport) static const int const_vars[2];
>   __declspec(dllexport) static int nonconst_vars[2];
> };
>
> const int Test::const_vars[2] = {11, 23};
> int Test::nonconst_vars[2] = {12, 24};
> ```
>
> D
> ```cpp
> extern(C++, class) struct Test
> {
>     extern export static __gshared const(int)[2] const_vars;
>     extern export static __gshared int[2] nonconst_vars;
> }
> ```
>
> I get the nonconst_vars to link correctly, but for the 
> const_vars the mangled name contains one less const than what 
> MSVC generates, so I assume I have to wrap this somehow 
> differently. I tried "const(int[2])" but that didn't make a 
> difference.
>
> D wants to link against:
> `?const_vars at Test@@2PAHB`
>
> which is:
> `public: static int * const Test::const_vars" ()`
>
> MSVC exports:
> `?const_vars at Test@@2QBHB`
>
> which is:
> `public: static int const * const Test::const_vars`
>
>
> Any idea?

That looks like another bug in the compiler. pragma(mangle, ...) 
should work as a workaround.


More information about the Digitalmars-d-learn mailing list