www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C++ bindings: const(T) dropped in return types of templates ?

reply Jan <Jan Krassnigg.de> writes:
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 M  SA?AU1 XZ`

However, I am using castXml to extract my C++ information, and 
that says that the mangled name should be:
`?ZeroVector ?$Vec3Template M  SA?BU1 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
Dec 06 2021
parent reply frame <frame86 live.com> writes:
On Monday, 6 December 2021 at 20:31:47 UTC, Jan wrote:

 So am I missing something, or did the compiler somehow forget 
 about the const-ness?
Sounds like a bug to me, eg this one: https://issues.dlang.org/show_bug.cgi?id=20685
Dec 08 2021
parent reply frame <frame86 live.com> writes:
On Thursday, 9 December 2021 at 07:41:32 UTC, frame wrote:
 On Monday, 6 December 2021 at 20:31:47 UTC, Jan wrote:

 So am I missing something, or did the compiler somehow forget 
 about the const-ness?
Sounds like a bug to me, eg this one: https://issues.dlang.org/show_bug.cgi?id=20685
But this is no show stopper, you can always force the mangling with the pragma directive. https://dlang.org/spec/pragma.html#mangle
Dec 08 2021
parent Jan <Jan Krassnigg.de> writes:
On Thursday, 9 December 2021 at 07:58:46 UTC, frame wrote:
 On Thursday, 9 December 2021 at 07:41:32 UTC, frame wrote:
 On Monday, 6 December 2021 at 20:31:47 UTC, Jan wrote:

 So am I missing something, or did the compiler somehow forget 
 about the const-ness?
Sounds like a bug to me, eg this one: https://issues.dlang.org/show_bug.cgi?id=20685
But this is no show stopper, you can always force the mangling with the pragma directive. https://dlang.org/spec/pragma.html#mangle
Oh it's a known bug for over a year already :( It is a bit of a show stopper for me, since the mangled name that I have isn't always correct (CastXML uses Clang and that seems to pick different calling conventions sometimes, than what MSVC would do, so the mangled names that I get are not guaranteed to be correct). Well, I'll work around it for now, thanks for the answer.
Dec 09 2021