[dmd-beta] rvalue references
Andrej Mitrovic
andrej.mitrovich at gmail.com
Thu Apr 12 14:24:17 PDT 2012
On 4/12/12, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> Why do you need more than just normal pointers? Because of this proposal
> making taking the address of a ref illegal?
Yeah. For example:
extern(C) void c_modify_int(int* x) { }
void modify_int(ref int x)
{
c_modify_int(&x);
}
void main()
{
int x;
modify_int(x);
}
Currently you don't have to use address-of operator if you mark the
extern(C) function as taking ref:
extern(C) void c_modify_int(ref int x) { }
void modify_int(ref int x)
{
c_modify_int(x);
}
But if both of these get banned, the only way to pass by reference
while still keeping the same API would be to create a new temporary:
extern(C) void c_modify_int(int* x) { }
void modify_int(ref int x)
{
int y = x;
c_modify_int(&y);
x = y;
}
Can this be optimized by the compiler? I don't know. It sure makes for
some ugly code, especially if the C function takes multiple pointer
arguments. I don't know how many libraries depend on working 'ref' on
C functions and/or taking address of a reference, but banning 'ref' on
C functions would break the existing wxD bindings.
More information about the dmd-beta
mailing list