www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Variadic template bug ?

reply Funog <funog ifrance.com> writes:
The following code :


import std.stdio;

void Test(literal...)()
{
    foreach(i, foo ; literal)
    {
        static if(foo[0] == '$')
            writefln(foo);
    }
}

void main()
{
    Test!("top", "$tip", "tap")(); //Should output "$tip"
}

won't compile :

jakuz.d(8): Error: expression cast(int)(foo[0u]) == 36 is not constant or does
not evaluate to a bool
jakuz.d(8): Error: expression cast(int)(foo[0u]) == 36 is not constant or does
not evaluate to a bool
jakuz.d(8): Error: expression cast(int)(foo[0u]) == 36 is not constant or does
not evaluate to a bool
jakuz.d(15): template instance jakuz.Test!("top","$tip","$tap") error
instantiating


It works by replacing "foo" by "literal[i]" in the static if statement...


import std.stdio;

void Test(literal...)()
{
    int r;
    foreach(i, foo ; literal)
    {
        static if(literal[i][0] == '$')
            writefln(foo);
    }
}

void main()
{
    Test!("top", "$tip", "tap")();
}

Now outputs:
$tip


But shouldn't foo and literal[i] be exactly the same ?
Oct 25 2007
parent BCS <ao pathlink.com> writes:
Reply to Funog,

 The following code :
 
 import std.stdio;
 
 void Test(literal...)()
 {
 foreach(i, foo ; literal)
 {
 static if(foo[0] == '$')
 writefln(foo);
 }
 }
 void main()
 {
 Test!("top", "$tip", "tap")(); //Should output "$tip"
 }
 won't compile :
 
 jakuz.d(8): Error: expression cast(int)(foo[0u]) == 36 is not constant
 or does not evaluate to a bool
 
 jakuz.d(8): Error: expression cast(int)(foo[0u]) == 36 is not constant
 or does not evaluate to a bool
 
 jakuz.d(8): Error: expression cast(int)(foo[0u]) == 36 is not constant
 or does not evaluate to a bool
 
 jakuz.d(15): template instance jakuz.Test!("top","$tip","$tap") error
 instantiating
 
 It works by replacing "foo" by "literal[i]" in the static if
 statement...
 
 import std.stdio;
 
 void Test(literal...)()
 {
 int r;
 foreach(i, foo ; literal)
 {
 static if(literal[i][0] == '$')
 writefln(foo);
 }
 }
 void main()
 {
 Test!("top", "$tip", "tap")();
 }
 Now outputs:
 $tip
 But shouldn't foo and literal[i] be exactly the same ?
 
yes, this is an old bug, I don't recall if it is in the bugzilla yet. If it's not, go ahead and add it. Walter: Can we get this fixed already?!!?
Oct 25 2007