www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - static if doesn't use short-circuit evaluation for &&.

reply Don Clugston <dac nospam.com.au> writes:
The docs do not indicate that 'static if' behaves quite differently to 
'if'. The following example should compile, but it fails with an access 
violation.
--------------
template zebra(char [] w)
{
   static if (w.length>2 && w[1]=='q') const bool zebra = true;
   else const bool zebra = false;
}

const bool lion = zebra!("a");
--------------
Workaround:

Unlike the situation with if(), you cannot just split the condition into 
two parts. There's no 'return'/'exit template' statement. And you can't 
store the

Because the example above doesn't compile, this has to be rewritten as

template zebra(char [] w)
{
   static if (w.length>2) {
       static if (w[1]=='q') const bool zebra = true;
       else const bool zebra = false;
   } else const bool zebra = false;
}

Note the duplicated code. If the actions for true and false are more 
complicated, it becomes unworkable, and it is necessary to replace the 
condition with a template returning a bool, just like the one above.
Jan 20 2006
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Don Clugston wrote:
 The docs do not indicate that 'static if' behaves quite differently to 
 'if'. The following example should compile, but it fails with an access 
 violation.
 --------------
 template zebra(char [] w)
 {
   static if (w.length>2 && w[1]=='q') const bool zebra = true;
   else const bool zebra = false;
 }
 
 const bool lion = zebra!("a");
<snip> There are two bugs here. The first is that the compiler doesn't honour short-circuit evaluation in a static if, and the second is that it doesn't do array bounds checking correctly. Simply fixing the first would make that compile, but would still mean that template zebra(char [] w) { static if (w[1]=='q') const bool zebra = true; else const bool zebra = false; } const bool lion = zebra!("a"); crashes the compiler. However, under GDC 0.17 I get (for your code) static_if.d:3: string index 1 is out of bounds static_if.d:7: template instance static_if.zebra!("a") error instantiating So GDC has only the first of the two bugs. Strange. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jan 20 2006
parent Don Clugston <dac nospam.com.au> writes:
Stewart Gordon wrote:
 Don Clugston wrote:
 
 The docs do not indicate that 'static if' behaves quite differently to 
 'if'. The following example should compile, but it fails with an 
 access violation.
 --------------
 template zebra(char [] w)
 {
   static if (w.length>2 && w[1]=='q') const bool zebra = true;
   else const bool zebra = false;
 }

 const bool lion = zebra!("a");
<snip> There are two bugs here. The first is that the compiler doesn't honour short-circuit evaluation in a static if, and the second is that it doesn't do array bounds checking correctly. Simply fixing the first would make that compile, but would still mean that template zebra(char [] w) { static if (w[1]=='q') const bool zebra = true; else const bool zebra = false; } const bool lion = zebra!("a"); crashes the compiler. However, under GDC 0.17 I get (for your code) static_if.d:3: string index 1 is out of bounds static_if.d:7: template instance static_if.zebra!("a") error instantiating So GDC has only the first of the two bugs. Strange.
No, it's exactly the same on DMD. It doesn't crash the compiler. It just doesn't compile. Momentary lapse of reason. Sorry for the confusion.
Jan 20 2006