Proposal: static template(fail)

BCS ao at pathlink.com
Fri Dec 14 11:01:58 PST 2007


Reply to Jason,

> Janice Caron wrote:
> 
>> On 12/13/07, Jason House <jason.james.house at gmail.com> wrote:
>> 
>>> If more than one matches successfully, the code wasn't coded well
>>> enough.
>>> 
>> But then you'd have to do
>> 
>> template A(T:int) {/*code*/}
>> template A(T:anything except an int){/*code*/}
> You can do the following for something so simple
> template A(T){
> static if (T:int){/*code*/}
> else {/*code*/}
> }

the problem with static if is that it turns into a work-or-die-hard solution. 
Once the static if is gotten to, there is no way to back out and say "I don't 
known how to handle this". The template(fail) would allow for complex static 
logic inside a template that can then be counted as a "this doesn't match, 
try other things" cases. This would come in handy in cases where you are 
are making a special cases handler template but want to be able to also have 
other templates be tried.

Example (each case maintained independently)


template Foo(T)
{
  static if(/*logic 1 on T*/)
    /* code */
  else
    template(fail);
}

template Foo(T)
{
  static if(/*logic 2 on T*/)
    /* code */
  else
    template(fail);
}

template Foo(T)
{
  /* general case code */
}





More information about the Digitalmars-d mailing list