Why can't we use strings in C++ methods?

Emmanuel Danso Nyarko emmankoko519 at gmail.com
Sat Nov 4 13:45:56 UTC 2023


On Saturday, 4 November 2023 at 12:34:28 UTC, Dadoum wrote:
> On Saturday, 4 November 2023 at 12:01:11 UTC, Emmanuel Danso 
> Nyarko wrote:
>> [...]
>>
>> So C-strings are just an array of characters that are governed 
>> by simple functions and D strings also defined the same. So 
>> you could see that D strings are possibly built on the 
>> architecture of C strings. In C++, string is a complex 
>> standard template library that generates complex symbols and 
>> cannot be mapped as it is based on a standard library 
>> implementation.
>
> I don't think the strings are being translated in any way when 
> we change the linkage. Strings in D are dynamic arrays of 
> chars, the C binding code is giving us a template representing 
> D's dynamic arrays, and so I thought that we could also use it 
> in C++ for interoperability with D dynamic arrays.

There is a syntax disagreement here that's why the D compiler is 
instantly stopping you from doing any symbol generated 
interaction with string in C++ interop. C++ doesn't know 'string' 
and C++ mangles with parameters and so passing string will make 
string get involved with the symbol generation and since 
string(std::string) in C++ is a template library, the D compiler 
stops you from engaging with 'string'



More information about the Digitalmars-d-learn mailing list