Consider the following C++ and D code:
//////////// testtailconstcpp.cpp ////////////////
class C
{
public:
virtual ~C();
};
C::~C()
{
}
void test(const C *c)
{
}
////////////// testtailconstd.d //////////////////
extern(C++) class C
{
~this();
}
extern(C++) void test(const C c);
void main()
{
test(new C);
}
//////////////////////////////////////////////////
The code compiles and links successfully on Linux, but fails to link on Windows. The following commands are used for Windows with Visual C++:
cl -c testtailconstcpp.cpp
dmd testtailconstd.d testtailconstcpp.obj
It results in the following error message:
testtailconstd.obj : error LNK2019: unresolved external symbol "void __cdecl test(class C const * const)" (?test@@YAXQEBVC@@@Z) referenced in function _Dmain
Hint on symbols that are defined and could potentially match:
"void __cdecl test(class C const *)" (?test@@YAXPEBVC@@@Z)
testtailconstd.exe : fatal error LNK1120: 1 unresolved externals
Error: linker exited with status 1120
It does not work, because D mangles the parameter type as full const, while the C++ function uses head const. The mangling could simply be changed, but that would break code, which really needs full const. It works on Linux, because the outer const is already ignored for the mangling.
A workaround is to use pragma(mangle, ...).
Comment #1 by dlang-bot — 2021-11-28T11:37:12Z
@tim-dlang created dlang/dmd pull request #13369 "Fix issue 22550 - tail const C++ class not usable on Windows" fixing this issue:
- Fix issue 22550 - tail const C++ class not usable on Windows
This changes the C++ mangling of const(Class) from "Class const * const"
to "Class const *" for Windows, but only if the class type is used at the
top of a type. It is a breaking change for code using "Class const * const"
in C++, but this code seems to be less common.
https://github.com/dlang/dmd/pull/13369
Comment #2 by robert.schadek — 2024-12-13T19:19:28Z