Bug 2379 – CTFE fails unless it is very straightforward

Status
RESOLVED
Resolution
INVALID
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-09-29T18:35:00Z
Last change time
2015-06-09T01:20:16Z
Assigned to
bugzilla
Creator
2korden

Comments

Comment #0 by 2korden — 2008-09-29T18:35:17Z
First attempt: ----------- template Test0(invariant(char)[] text) { auto Test0 = text; } writefln(Test0!("hello")); // success pragma(msg, Test0!("hello")); // failure: // pragma msg string expected for message, not 'Test0' Let's replace invariant(char)[] with alias :( ----------- template Test0(alias text) { auto Test0 = text; } pragma(msg, Test0!("hello")); // success Let's make it slightly more complex: ----------- template Test0(alias text) { auto Test0 = text[0..1]; } pragma(msg, Test0!("hello")); // failure: // pragma msg string expected for message, not 'Test0' Other example: ----------- template Test0(invariant(char)[] text) { auto Test0 = text; } template Test1(invariant(char)[] text) { auto Test1 = Test0!(text); } writefln(Test0!("hello")); // success writefln(Test1!("hello")); // failure: // Error: non-constant expression Test0
Comment #1 by shro8822 — 2008-09-29T19:02:54Z
Something is wrong here. The examples don't use CTFE. Is the title wrong?
Comment #2 by snake.scaly — 2008-09-30T07:56:30Z
Everything works as expected. template Test0(invariant(char)[] text) { auto Test0 = text; } effectively declares invariant(char)[] Test0 = "hello"; which is a variable initialized at runtime. The correct way to write this template is template Test0(invariant(char)[] text) { enum Test0 = text; } which declares a manifest constant.
Comment #3 by bugzilla — 2008-10-02T04:05:03Z
Sergey is correct.