Passing structs to functions
Namespace via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jul 2 14:05:18 PDT 2016
On Saturday, 2 July 2016 at 19:40:53 UTC, phant0m wrote:
> On Saturday, 2 July 2016 at 19:25:37 UTC, ketmar wrote:
>> note the first "()", though: this is effectively a template
>> function, which compiler will instantiate either with "ref" or
>> without it.
>
> Yeah, I've noticed it. Always using function template for this
> use case seems like a weird idea.
Try this little trick:
----
import std.stdio;
struct A {
private A* _this = null;
public int id = 0;
this(int id) {
this.id = id;
}
ref A byRef() {
_this = &this;
return *_this;
}
}
void foo(ref const A a) {
writeln(a.id);
}
void main() {
foo(A(42).byRef());
}
----
More information about the Digitalmars-d-learn
mailing list