What on earth is a ref function?
simendsjo
simen.endsjo at pandavre.com
Sun Aug 8 15:25:58 PDT 2010
The spec is very short here, and the example doesn't give me much..
// I thought "allows functinos to return by reference" meant it could
return local variables..
ref int* ptr() {
auto p = new int;
*p = 12;
return p; // Error: escaping local variable
}
// So whats the difference between these functions?
ref int val() {
auto p = new int;
assert(*p == 0);
*p = 10;
assert(*p == 10);
return *p;
}
int val2() {
auto p = new int;
*p = 10;
return *p;
}
unittest
{
assert(val() == 10);
assert(val2() == 10);
auto retvalue = val() = 99; // References can be lvalues.. What?
assert(retvalue == 99);
}
More information about the Digitalmars-d-learn
mailing list