Bug 2849 – Length of a local immutable(char[]) cannot be a template value argument
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2009-04-17T16:57:00Z
Last change time
2015-06-09T01:18:03Z
Keywords
rejects-valid
Assigned to
nobody
Creator
clugdbug
Comments
Comment #0 by clugdbug — 2009-04-17T16:57:38Z
struct bar(int n){}
void main(){
immutable(char[]) x = "bug";
bar!(x.length) z;
}
-----
fog.d(3): Error: expression (x = "bug").length is not a valid template value arg
ument
fog.d(4): Error: template instance fog.bar!((x = "bug").length) error instantiat
ing
==========
Yet here are two very similar cases which work:
struct bar(int n){}
immutable(char[]) x1 = "bug";
void main(){
immutable(char[]) x2 = "bug";
enum q= x2.length;
bar!(x1.length) z; // ok, it's a global
bar!(q) z2; //ok, it's been copied to an enum
}
Probably related to issue #2841 and issue #2414.
Comment #1 by clugdbug — 2012-03-15T22:40:16Z
It's because it doesn't try to const fold it. In this case,
the init value is constant, so it could possibly work. In other cases,
it could be initialized from something non-constant (eg, a function
parameter). Regardless, it should say that it's not constant, rather
than just that it's not a valid template parameter.
Comment #2 by andrej.mitrovich — 2012-12-25T08:06:48Z
(In reply to comment #1)
> It's because it doesn't try to const fold it. In this case,
> the init value is constant, so it could possibly work. In other cases,
> it could be initialized from something non-constant (eg, a function
> parameter). Regardless, it should say that it's not constant, rather
> than just that it's not a valid template parameter.
That first sample now works, but w.r.t. errors I can't reproduce the bad error message. E.g.:
struct bar (int n){}
void main()
{
char[] x = "bug".dup;
bar!(x.length) z;
}
test.d(5): Error: variable x cannot be read at compile time
test.d(5): Error: expression x.length is not a valid template value argument
test.d(5): Error: template instance test.bar!(x.length) error instantiating
Fixed?