Passing to c++ std::string and vector

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Apr 30 10:48:40 UTC 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



More information about the Digitalmars-d-learn mailing list