Using a C++ class in a D associative array
Jacob Carlborg
doob at me.com
Wed Aug 22 19:25:40 UTC 2018
On 2018-08-21 02:07, kinke wrote:
> 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.
This could be solved, I think, with having "TypeInfo.getHash" a template
taking the actual type and not void*. That template can then inspect if
the passed type is a D class or any other type of class and act accordingly.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list