foreach on a tuple in CTF

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Tue Apr 24 16:37:04 PDT 2007


Max Samukha wrote:
> Why the function cannot be evaluated at compile time? Is it a bug? 
> 
> import std.stdio;
> 
> char[] foo(A...)()
> {
>     char[] s = "";
> 
>     foreach (i, v; A)
>     {
>         static if (i == 1)
>             s = i.stringof;
>     }
> 
>     return s;
> }
> 
> void main(char[][] args)
> {
>     alias foo!("test", 100) f;
> 
>     writefln(f());		// works				
>     pragma(msg, f());	// error: cannot evaluate at compile time
> }

Probably this is a bug too. I haven't really used CTFE much yet, but
here's a smaller version of the above that triggers the same error msg
with dmd 1.013

  char[] foo(A...)()
  {
    foreach (i, v; A) {}
    return "";
  }

  pragma(msg, foo!("a")());   // error

It works if you remove the index part from the foreach or change "a" to
e.g. 42.


More information about the Digitalmars-d-learn mailing list