Bug 4284 – empty string[] alias lacks .length in a template

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-06-06T06:43:00Z
Last change time
2015-06-09T05:10:40Z
Keywords
patch, rejects-valid
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-06-06T06:43:17Z
This looks like a correct D2 program: import std.string: split; template Foo(alias items) { static if (items.length == 0) enum Foo = 1; else enum Foo = 2; } enum string[] items = split(""); static assert(Foo!(items) == 1); void main() {} But DMD v2.046 prints at compile-time: test.d(3): Error: expression (null.length) == 0u is not constant or does not evaluate to a bool test.d(9): Error: template instance test.Foo!(items) error instantiating test.d(9): Error: static assert (2 == 1) is false ------------------------ To fix it you have to test for null too: import std.string: split; template Foo(alias items) { static if (items == null || items.length == 0) enum Foo = 1; else enum Foo = 2; } enum string[] items = split(""); static assert(Foo!(items) == 1); void main() {}
Comment #1 by yebblies — 2011-09-05T21:47:59Z
Comment #2 by bugzilla — 2011-09-17T17:19:59Z