const(Class) is mangled as Class const* const

Benjamin Thaut via Digitalmars-d digitalmars-d at puremagic.com
Sun Mar 26 03:43:11 PDT 2017


Consider the following C++ and D source:

class Class
{
	virtual ~Class(){}
};

// mangles as ?getClass@@YAPEAVClass@@XZ
Class * getClass() { return nullptr; }

// mangles as ?getClassConst@@YAPEBVClass@@XZ
const Class * getClassConst() { return nullptr; }

// mangles as ?getClassConstConst@@YAQEBVClass@@XZ
const Class * const getClassConstConst() { return nullptr; }

extern(C++)
{
   class Class
   {
     void _cppDtor() {}
   }

   // Mangles as ?getClass@@YAPEAVClass@@XZ
   Class getClass() {return null;}

   // Mangles as ?getClassConst@@YAQEBVClass@@XZ
   const(Class) getClassConst() {return null;}
}

As you see from the above example D mangles the getClassConst as 
a "Class const * const" instead of a "Class const *" ("YAQEBV" vs 
"YAPEBV"). Is this expected behavior? The core problem is that D 
can not express one of the two. Either const(Class) becomes 
"Class const *" or "Class const * const". I've never seen C++ 
code that returns const pointers to const classes so I think the 
default should be "Class const *". Either way its rather bad that 
we can only represent one or the other. Sooner or later someone 
will hit this problem again wanting the other option or both. Any 
idea how we can avoid changing C++ source code in order to bind 
it to D? Using pragma(mangle,) works but is kind of ugly. 
Especially because the string to pragma(mangle,) can't be 
genearted using CTFE.

Should I open a bug for this?


More information about the Digitalmars-d mailing list