Interfacing with C++, Objective-C and Other Languages

41.1 C++ Interop

Supports C++ name mangling, namespaces, classes (single inheritance), templates (instantiated in C++), and references. C++ templates can't be instantiated from D unless they're already instantiated.

Tools: dmd -HC (generate C++ headers from D), dpp (C++ headers), calypso (LDC fork, experimental).

// C++: namespace geo { class Shape { public: virtual double area(); }; double scale(double, double); }
extern(C++, "geo")
{
    double scale(double v, double k);
    class Shape { double area(); }              // virtual functions map to D virtual methods
}
extern(C++) struct CppStruct { int a; }
extern(C++, class) struct CppClassAsStruct {}    // mangling hint
import core.stdcpp.vector : vector;              // partial bindings to the C++ standard library
import core.stdcpp.string : basic_string;

41.2 Objective-C (macOS/iOS)

extern(Objective-C)
interface NSString
{
    static NSString alloc() @selector("alloc");
    NSString initWithUTF8String(in char* str) @selector("initWithUTF8String:");
    void release() @selector("release");
}

41.3 Other Languages

pyd, autowrap (generate Python extensions from D), nimpy-like: autowrap also targets Excel/C#/.NET LuaD, bindbc-lua

LDC WebAssembly target, spasm (DOM bindings) autowrap, C ABI JNI via extern(C), djni excel-d (xll add-ins)