Overlapping functionality: IFTI, templates, is-expressions

Jason House jason.james.house at gmail.com
Wed Mar 19 15:52:16 PDT 2008


Russell Lewis wrote:
> What I'm thinking is something like this:
> 
> ORIGINAL CODE (viewing template specialization as syntax sugar)
> 
>    template foo(T : int) { ... }
>    template foo(T : char) { ... }
> 
> REWRITTEN CODE (expressing specialization as is-expressions)
> 
>    template foo(TPL...)
>    {
>      static if(TPL.length == 1 && is( TPL[0] == int )) { ... }
>      else
>      static if(TPL.length == 1 && is( TPL[0] == char)) { ... }
>      else
>      static assert(false, "ERROR: Unsupported specialization of foo");
>    }
> 
> END CODE

Defining template specialization in terms of a single template with static
if's would be a breaking change.  Two reasons come to mind:
  1. Template instantiation changes from "best match" to "first match".
  2. SFINAE is no longer true.

#1 This would shift the work of template matching from the compiler writer
to the programmer.  Ordering of templates would become important to the
programmer.  The viability of this will partially depend on if someone come
up with an example where no single template ordering would work properly. 
If there's no examples or only pathological examples (that may break many
implementations anyway), I'd be fine with the loss of functionality.

#2 I know from past discussions that C++ programmers hold this very near and
dear to their heart.  Personally, it seems like a back door to long
compilation times.  I don't have any problem with enforcing criteria for
templates to be defined up front, but I'm certain others don't agree with
me.  Does the loss of SFINAE make templates too close to generics?  I know
C# will refuse to compile code if the up-front criteria for a generic type
isn't specific enough.



More information about the Digitalmars-d mailing list