On Saturday, 24 March 2018 at 23:03:36 UTC, John Colvin wrote:
Here is a small proof of concept I made to demonstrate how easy
it seems to be to use `auto ref` to call a C++ virtual const&
function without incurring any more copies than would happen
with the same calls from C++. I'm sure it could be improved a
lot, but does the basic concept match what you would need?
// D source file:
/// mix this in to your extern(C++) class with a list of the
functions where you
/// want to be able to pass rvalues to ref parameters.
auto rValueRefCalls(Funcs ...)()
{
string ret;
foreach (foo; Funcs)
ret ~= `extern(D) void ` ~ __traits(identifier, foo) ~
`(Args ...)(auto ref Args args)
if (__traits(compiles, (&this.` ~ __traits(identifier, foo)
~ `)(args)))
{
(&this.foo)(args);
}`;
return ret;
}
extern(C++)
{
class A
{
void foo(const ref int v);
mixin(rValueRefCalls!foo);
}
A makeA();
}
void main()
{
int x = 3;
auto a = makeA();
a.foo(x);
a.foo(3);
}
// C++ source file:
#include<cstdio>
class A
{
public:
virtual void foo(const int& v)
{
printf("%d\n", v);
}
};
A *makeA()
{
return new A;
}
That isn't going to scale to the number of potential functions
that are going to need this. It's going to cause slow compile
speeds and create a bloated binary.