Thread overview
Passing to c++ std::string and vector
Apr 30, 2018
NewUser
Apr 30, 2018
Uknown
Apr 30, 2018
Jonathan M Davis
Apr 30, 2018
NewUser
Apr 30, 2018
Seb
April 30, 2018
Hi,

How do I pass a d string to a c++ std::string?

NewUser
April 30, 2018
On Monday, 30 April 2018 at 01:07:35 UTC, NewUser wrote:
> Hi,
>
> How do I pass a d string to a c++ std::string?
>
> NewUser

There is no trivial way to do this as far as I'm aware, mostly due to C++ mangling issues in DMD. You can try calypso [0] or dpp [1], which might work. You can also see this PR[2]

[0]: https://wiki.dlang.org/Calypso
[1]: https://forum.dlang.org/post/kbcdppawjtcdkdtdzwwp@forum.dlang.org
[2]: https://github.com/dlang/druntime/pull/1316
April 30, 2018
On Monday, April 30, 2018 01:07:35 NewUser via Digitalmars-d-learn wrote:
> Hi,
>
> How do I pass a d string to a c++ std::string?

The most straightforward way would be to create a C or C++ function which accepts const char* and size_t and then creates the std::string, in which case you pass it arr.ptr (or &arr[0]) and arr.length from D (and you could create a D helper function that takes a dynamic array to then call the C/C++ function if you don't want to use the array properties directly every time).

Essentially, you'll probably end up creating wrapper functions for any C++ functions that you want to call that take std::string, since there is not currently a straightforward way to construct a std::string from D (there's Calypso, but using it means that you're tied to ldc). Solutions are likely forthcoming, but there's nothing production-ready at this point. Do interacts reasonably well when it's operatoring on pointers to C++ classes, but as soon as it has to deal with construction or destruction, things get more complicated (which means that C++ classes on the stack definitely get more complicated). You can still get things to work, but it's frequently not straightforward in the way that calling C functions is.

- Jonathan M Davis

April 30, 2018
On Monday, 30 April 2018 at 10:48:40 UTC, Jonathan M Davis wrote:
> On Monday, April 30, 2018 01:07:35 NewUser via Digitalmars-d-learn wrote:
>> Hi,
>>
>> How do I pass a d string to a c++ std::string?
>
> The most straightforward way would be to create a C or C++ function which accepts const char* and size_t and then creates the std::string, in which case you pass it arr.ptr (or &arr[0]) and arr.length from D (and you could create a D helper function that takes a dynamic array to then call the C/C++ function if you don't want to use the array properties directly every time).
>
> Essentially, you'll probably end up creating wrapper functions for any C++ functions that you want to call that take std::string, since there is not currently a straightforward way to construct a std::string from D (there's Calypso, but using it means that you're tied to ldc). Solutions are likely forthcoming, but there's nothing production-ready at this point. Do interacts reasonably well when it's operatoring on pointers to C++ classes, but as soon as it has to deal with construction or destruction, things get more complicated (which means that C++ classes on the stack definitely get more complicated). You can still get things to work, but it's frequently not straightforward in the way that calling C functions is.
>
> - Jonathan M Davis

Thanks.
April 30, 2018
On Monday, 30 April 2018 at 01:07:35 UTC, NewUser wrote:
> Hi,
>
> How do I pass a d string to a c++ std::string?
>
> NewUser

There's https://github.com/dlang/druntime/pull/1316 (an attempt at making C++ standard library)