Interfacing to const T& or const T* C++ code

Atila Neves via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 2 01:30:39 PDT 2014


On Sunday, 1 June 2014 at 09:18:50 UTC, bearophile wrote:
> Atila Neves:
>
>> D:
>>     struct Foo { int i; int j; }
>>     extern(C++) void useFoo(ref const(Foo) foo);
>>
>> C++:
>>     struct Foo { int i; int j; };
>>     void useFoo(const Foo& foo) { ... }
>
> This doesn't look very safe because D const is transitive, 
> unlike the C++ const. So in the C++ code you can mutate inner 
> data inside foo and the D code will assume such data is not 
> changed.
>
> Bye,
> bearophile

Oh, it's not even remotely safe and I know that. But it's "kinda" 
safe and it "works" for structs, so why not classes?

The worse thing about this is that I can happily slap a "const" 
in the D declaration and it'll link to non-const C++ code. That's 
a lot less safe than treating C++ const as if it were D const.

Atila


More information about the Digitalmars-d-learn mailing list