Templated function (free nor member) cannot return by-ref explicitly (i.e. not with auto ref) if instantiation arguments are infered and the instantiation arguments contain at least one value argument. This does not compile: -------------------- void main() { S s; s.opDispatch!"bar"(42); // fails } struct S { ref int opDispatch(string str, T)(T arg) { return _xyz; } int _xyz; } -------------------- % dmd -o- -c test test.d(8): Error: variable test.S.str only parameters or foreach declarations can be ref -------------------- The error does not occur if the template instantiation arguments are not infered: -------------------- void main() { S s; s.opDispatch!("bar", int)(42); // fine } ... -------------------- Auto ref is fine: -------------------- void main() { S s; s.opDispatch!"bar"(42); // fine } struct S { auto ref opDispatch(string str, T)(T arg) { return _xyz; } int _xyz; } -------------------- This is a workaround for opDispatch: -------------------- struct S { template opDispatch(string str) { ref int opDispatch(T)(T arg) { return _xyz; } } int _xyz; } --------------------
*** This issue has been marked as a duplicate of issue 3175 ***