Using a C++ class in a D associative array

kinke noone at nowhere.com
Tue Aug 21 00:07:40 UTC 2018


On Monday, 20 August 2018 at 22:16:09 UTC, Jacob Carlborg wrote:
> At the third line there's a call from 
> object.TypeInfo_Class.getHash. I looked up to see what the 
> "getHash" method is doing in druntime [2], the method looks 
> like this:
>
> override size_t getHash(scope const void* p) @trusted const
> {
>     auto o = *cast(Object*)p;
>     return o ? o.toHash() : 0;
> }

I guess the compiler uses the AA key type's TypeInfo, which is 
available for extern(C++) classes too. The 
TypeInfo_Class.getHash() then uses the dynamic type via virtual 
call, (wrongly) assuming it's always a D class. For an 
extern(C++) class, it will either call another virtual function 
(no inherited virtual functions from Object), what you were 
seeing, or attempt to call... something. ;)

> All this just compiled without any error or warnings. No 
> runtime exceptions or asserts were triggered. I just got a 
> really weird behavior.

This is somewhat special due to the common TypeInfo.getHash() 
signature for all kinds of types, and so just taking a hairy 
void* pointer to the byte/real/static array/AA/object/… to be 
hashed.

Polishing C++ interop with extern(C++) classes (matching 
ctors/dtors, mixed class hiearchies, ability to easily 
allocate/construct/destruct/free on the other language side etc.) 
has started with v2.081 and is still on-going; there are probably 
more places in druntime silently assuming a D class.


More information about the Digitalmars-d mailing list