link to C++ function in a namespace whose name is a D keyword

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 6 11:24:22 PST 2016


On 01/06/2016 10:35 AM, Carl Sturtivant wrote:
> Hello,
>
>  From D I want to call e.g.
>
> /* C++ prototype */
> namespace ns {
>      int try(int x);
> }
>
> without writing a C or C++ wrapper.
>
> Presumably the following D doesn't work, because it doesn't mangle the
> name as if it's in the namespace ns.
>
> pragma(mangle, "try") extern(C++, ns) int try_(int x);
>
> So how to I get correct mangling here?
>

This looks like an oversight to me. I tried the following workaround but 
it uses D mangling scheme:

import core.demangle;

// PROBLEM: No way of saying "mangle C++ name".
pragma(mangle, mangle!(int function(int))("ns::body"))
extern(C++, ns)
int body_(int x);

void main() {
     assert(ns.body_(42) == 42);
}

And yes, 'try' is a C++ keyword as well; so I moved to 'body. :)

Ali



More information about the Digitalmars-d-learn mailing list