H1 2015 - C++ integration

Guillaume Chatelet via Digitalmars-d digitalmars-d at puremagic.com
Sun Feb 15 11:44:38 PST 2015


On Sunday, 15 February 2015 at 15:37:33 UTC, Paolo Invernizzi 
wrote:
> That's a great work.

Thx :)

> Guillaume, I'll try to ask this question again.
>
> I'm wrapping the C++ API of OpenCV [1], and it works great.
> I'm using a D struct for std::string instead of D class, 
> because I agree with your findings that's more convenient.
>
> But, in case I would like to use a D class for std::string, how 
> to call a C++ function that takes a std::string&? It's feasible 
> right now?
>
> I think we need more control over the mangle in extern(C++)...
>
> [1] http://docs.opencv.org/trunk/modules/refman.html

I just pushed a new commit which shows how to do it with classes 
as well:
https://github.com/gchatelet/dlang_cpp_std/commit/892a736386ecd84516b7330fef4ee75f1b4d2ad3

This requires to pervert the type system though so it's pretty 
unsafe.
I added the two following helper functions to reinterpret a D 
reference semantic as a C++ value semantic :
const (basic_string*) c_ptr() const { return cast(const 
std_string*)(this); }
ref const(basic_string) c_ref() const { return *cast(const 
std_string*)(this); }

You can then call the C++ function :
getStringSize(s.c_ref);

This is cumbersome and unsafe but workable.

Because the C++ function contains a const ref to string we have 
to provide the correct mangling manually anyways because of 
mangling bug https://issues.dlang.org/show_bug.cgi?id=14178


More information about the Digitalmars-d mailing list