import std.metastrings; extern (C++){ interface D{ int bar(int i, int j, int k); } } extern(C){ void* _Znwj(uint); void _ZdlPv(void*); } template cpp_decl(T){ const cpp_decl = "extern(C) extern char* _ZTV"~ToString!(T.stringof.length)~T.stringof~";"; } template cpp_constructor(T, ARGS...){ const cpp_constructor = "extern(C) void _ZN"~ToString!(T.stringof.length)~T.stringof~"C1Ev(void*);";//fixme: mangle and type } template cpp_destructor(T){ const cpp_destructor = "extern(C) void _ZN"~ToString!(T.stringof.length)~T.stringof~"D1Ev(void*);"; } T cpp_new(T, ARGS...)(ARGS args){ const fn="_ZN"~ToString!(T.stringof.length)~T.stringof~"C1Ev";//fixme: mangle void* rv; rv = _Znwj(T.sizeof); static if(is(typeof(mixin(fn)))){ mixin("alias "~fn~" init;"); init(rv, args); }else static if(ARGS.length==0){ *cast(void**)rv = mixin("&_ZTV"~ToString!(T.stringof.length)~T.stringof)+2; }else static assert(0); return cast(D)rv; } void cpp_delete(T)(T obj){ const fn="_ZN"~ToString!(T.stringof.length)~T.stringof~"D1Ev"; static if(is(typeof(mixin(fn)))){ mixin("alias "~fn~" term;"); term(cast(void*)obj); } _ZdlPv(cast(void*)obj); } mixin(cpp_decl!(D)); mixin(cpp_constructor!(D));//if D has constructor mixin(cpp_destructor!(D));//if D has destructor void main(){ D d = cpp_new!(D); d.bar(9,10,11); cpp_delete(d); }