I tried this on the DMD64 D compiler v2.055 on linux. The valid D code at the bottom fails with the following errors when I invoke ---------- $ dmd template_autoref_bug.d template_autoref_bug.d(3): Error: auto can only be used for template function parameters ---------- I found three independent ways to suppress this bug (mentioned also in the comments below). 1. I introduce line A. 2. Replace "auto ref" with "const ref". 3. Add a dummy template parameter with default value to the signature of mat_mult_ret() as mentioned in the code comments. The fact that the above workarounds actually work seems to indicate that it is not an error in my understanding of "auto ref", but an actual bug. ---------- struct MatrixT(int M, int N) { } void mat_mult_ret(int M,int N,int P)( auto ref MatrixT!(M,N) A, auto ref MatrixT!(N,P) B, ref MatrixT!(M,P) ret) { } void main(string[] args) { MatrixT!(2,2) mat22; MatrixT!(3,2) mat32; MatrixT!(2,3) mat23; // Line B by itself => compiler says: // template_autoref_bug.d(3): Error: auto can only be used for // template function parameters // To suppress bug (any one works): // * Lines A and B together => no error // * Line A by itself => no error // * Replace "auto ref" with "const ref" // * Add dummy parameter to signature of function: // void mat_mult_ret(int M,int N,int P, dum=void)(...) {...} //mat_mult_ret(mat23, mat32, mat22); // line A mat_mult_ret!(2,3,2)(mat23, mat32, mat22); // line B } ----------
Tested on dmd git HEAD, Linux/64, the compiler now accepts this code (including various variations as suggested in the comments). Looks like it's fixed since. Please reopen the bug if the problem still occurs. Thanks!