www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22550] New: tail const C++ class not usable on Windows

https://issues.dlang.org/show_bug.cgi?id=22550

          Issue ID: 22550
           Summary: tail const C++ class not usable on Windows
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: tim.dlang t-online.de

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, ...).

--
Nov 27 2021