:? in templates

Max Samukha spambox at d-coding.com
Wed Nov 18 03:34:08 PST 2009


On Wed, 18 Nov 2009 03:10:57 -0800, Bill Baxter <wbaxter at gmail.com>
wrote:

>Didn't this used to work?
>
>template factorial(int i) {
>    enum factorial = (i==0) ? 1 : i*factorial!(i-1);
>}
>
>With DMD 2.036 I'm getting:
>  Error: template instance factorial!(-495) recursive expansion
>
>Seems like it expands both branches regardless of the condition.
>And seems to me like it shouldn't.
>
>--bb

While we are at it, binary logical operators has the same issue:

static if (foo!() && bar!())
{
}
else
{
}

bar is instantiated even if 'foo!()' results in false. To work around
it, you have to add another 'static if' and duplicate the 'else'
block:

static if (foo!())
{
   static if (bar!())
   {
   }
   else
  {
  }
}
else
{
}

I've encountered the problem several times and would be happy to have
it fixed, if possible.



More information about the Digitalmars-d mailing list