Trouble understanding crash when class is returned by value from C++

Iain Buclaw ibuclaw at ubuntu.com
Mon Sep 3 10:29:29 PDT 2012


On 3 September 2012 18:15, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> On 9/3/12, Iain Buclaw <ibuclaw at ubuntu.com> wrote:
>> Indeed,  C++ classes are always passed in memory by design.  Whereas
> pointers could be passed in registers.
>
> That's cool. I learn something new every day. :)
>
>> And this is one
>> reason why you need to ensure that function signatures match in both D
>> and C/C++ code.
>
> Yeah that's doable when the type is a POD but when it's a class
> returned by value there is no equivalent in D since D classes are
> always references, so I can't match the D function signature to the C
> one.
>
>> extern "C"
>> FileName value_FileName(void* refVal)
>> {
>>     return *(FileName*)refVal;
>> }
>
> That won't work either since FileName is still in the return type and
> I can't match the function signature on the D side (it still crashes).
> The only thing I can think of is to match the C++ function signature
> to the D side via something like:
>

Ah, sorry, my bad.  I was testing marking D structs as addressable
(meaning are always passed in memory) whilst in the middle of looking
at the difference between D and C++ codegen. Must have left that
turned on still in my copy of gdc. ;-)


-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';


More information about the D.gnu mailing list