ref lost to opDispatch

Namespace rswhite4 at googlemail.com
Sat Jul 27 08:43:47 PDT 2013


On Saturday, 27 July 2013 at 15:09:44 UTC, Carl Sturtivant wrote:
>
> I ran into the following behavior.
>
> ==============================================
> void main() {
> 	int x = 100;
>
> 	B().modify( x, 99);
> 	writeln( x); //still 100
>
> 	modify( x, 99);
> 	writeln( x); //now is 99
> }
>
> void modify( ref int x, int y) { x = y; }
>
> struct B {
> 	void opDispatch(string f, Args...)( Args args) {
> 		mixin( f~"(args);");
> 	}
> }
> ==============================================
>
> When dispatched through a general argument tuple, ref is lost 
> and the dispatched code therefore does not have the desired 
> side effect. Is there anything I can do about this?

void opDispatch(string f, Args...)(auto ref Args args) {
     mixin( f~"(args);");
}


More information about the Digitalmars-d-learn mailing list