import std.stdio;
// This works
//ref int x(int[3] p) { return p[0]; }
// This works
//ref T x(T)(T[3] p) { return p[0]; }
// This does not work
//ref int x(size_t N)(int[N] p) if ( N > 0 ) { return p[0]; }
// This does not work
ref T x(T, size_t N)(T[N] p) if ( N > 0 ) { return p[0]; }
int main()
{
auto a = [1, 2, 3];
a.x = 42;
writeln( a );
return 0;
}
----
$ rdmd test.d
test.d(12): Error: variable test.N only parameters or foreach declarations can b
e ref
C:\d\dmd.2.031\dmd2\windows\bin\rdmd.exe: Couldn't compile or execute test.d.
Comment #1 by lat7h — 2009-11-18T10:30:44Z
This is only a problem with template parameter deduction:
ref int foo(int x)(int[x] v) if (x>0) { return v[0]; }
foo!(2)([1,2]); // works
foo([1,2]); // gives "only parameters or foreach..." error message
It can be sidestepped by explicit template declaration:
template foo(int x) if (x>0) {
ref int foo(int[x] v) { return v[0]; }
}
foo([1,2]); // works
While obviously not as nice as function template syntax, this workaround is not difficult to read or write and contains full functionality.
I posit the error message is wrong either way: declarations.c line 908 should read:
error("only function return types, parameters, and foreach declarations can be ref");
Comment #2 by rsinfu — 2010-09-20T22:03:05Z
*** Issue 4446 has been marked as a duplicate of this issue. ***
Comment #3 by rsinfu — 2010-09-25T18:16:35Z
Patch against dmd r687. deduceFunctionTemplateMatch() must reset storage class in parameter's scope as done in matchWithInstance().
--- a/src/template.c
+++ b/src/template.c
@@ -887,6 +887,7 @@ MATCH TemplateDeclaration::deduceFunctionTemplateMatch(Scope *sc, Loc loc, Objec
ScopeDsymbol *paramsym = new ScopeDsymbol();
paramsym->parent = scope->parent;
Scope *paramscope = scope->push(paramsym);
+ paramscope->stc = 0;
TemplateTupleParameter *tp = isVariadic();
int tp_is_declared = 0;
Increased the severity as the bug has been repeatedly reported: bug 4041, bug 4446, bug 4934 and comment 2 of bug 2460.
(In reply to comment #4)
> Applied the patch http://www.dsource.org/projects/dmd/changeset/695 but the bug
> remains.
Are you saying the bug remains since the reproducing code in comment #0 doesn't compile? It's because the type of [1,2,3] has changed to dynamic int[]. Replacing 'auto' to 'int[3]' would make it compile.
Comment #6 by clugdbug — 2010-11-23T05:15:10Z
(In reply to comment #5)
> (In reply to comment #4)
> > Applied the patch http://www.dsource.org/projects/dmd/changeset/695 but the bug
> > remains.
>
> Are you saying the bug remains since the reproducing code in comment #0 doesn't
> compile? It's because the type of [1,2,3] has changed to dynamic int[].
> Replacing 'auto' to 'int[3]' would make it compile.
Indeed it does work in DMD2.050.