C++ bindings: const(T) dropped in return types of templates ?

Jan Jan at Krassnigg.de
Mon Dec 6 20:31:47 UTC 2021


I am trying to auto-generate bindings for C++ code in D. I've 
come across something that looks like a bug in DMD to me, but 
since I'm such a newbie in D maybe I am missing something.

My C++ class looks like this:

```cpp
template <typename Type>
struct Vec3Template
{
   static const Vec3Template<Type> ZeroVector();
};
```

And my D binding code looks like this:

```cpp
extern(C++) struct Vec3Template(TYPE)
{
   static const(Vec3Template!(TYPE)) ZeroVector();
}
alias Vec3 = Vec3Template!(float);
```

However, when I try to use Vec3.ZeroVector() I am getting a 
linker error about unresolved symbols. It works with other 
functions, the error is specific to this function.

Now it complains that it can't find this one:
`?ZeroVector@?$Vec3Template at M@@SA?AU1 at XZ`

However, I am using castXml to extract my C++ information, and 
that says that the mangled name should be:
`?ZeroVector@?$Vec3Template at M@@SA?BU1 at XZ`

Running both names through undname.exe, an MSVC tool that 
generates the undecorated function name from the mangled name, it 
says that the latter function definition should be:
`public: static struct Vec3Template<float> const __cdecl 
Vec3Template<float>::ZeroVector(void)`

Whereas the former definition would be:
`public: static struct Vec3Template<float> __cdecl 
Vec3Template<float>::ZeroVector(void)`

So the one that D tries to link against is missing the `const`.

However, unless I misunderstood how to apply const to a type in 
D, you can see that I did wrap the type in const(). (I also tried 
immutable, but that's not allowed for extern C++ code).

So am I missing something, or did the compiler somehow forget 
about the const-ness?

I'm currently using DMD64 D Compiler v2.098.0-dirty


More information about the Digitalmars-d-learn mailing list