"SFINAE is Evil"

Christopher Wright dhasenan at gmail.com
Sat Mar 22 05:14:05 PDT 2008


Jason House wrote:
> I should probably expand this question...
> 
> Who uses templates in D? What is the most complex use you've done? Do you ever plan to get more complex? Would SFINAE make your life easier or harder?

Templates are utterly necessary and horribly untestable. I use them, but 
I try to keep them as braindead simple as possible.

I've done a reasonable amount of work with simple templates, eg DUnit's 
assertions, and some slightly more complex stuff, as with DMocks, but 
nothing incredibly difficult.

DMocks was mainly a matter of looping through reflection information and 
assembling some strings based on that, which wasn't so difficult; the 
hardest thing (besides debugging CTFE failures and oddities with mixins, 
but I did my best to limit the amount of both) was not being able to 
have a variable whose type was an empty tuple. I had to wrap the tuple 
in a templated class that would just not instantiate the arguments 
variable with an empty tuple.

SFINAE isn't a problem for me -- I don't need to overload templates 
much. If I did, I'd do it like this:

template Foo(T, U, V...)
{
    static if (condition1)
    {
       alias FooSpecialization1!(T, U, V) Foo;
    }
    else
    // ...
}

The only thing the compiler could do to help me that it isn't doing is 
to make it an error to have overloaded templates.

> I've written templates classes that have one definition, and used static if as needed for specialization. I don't forsee more complex usage and view SFINAE as something that would mask bugs.

Same. The only issue is with templates from different modules or libraries.

In this case, it could be helpful for the compiler to have an error 
message along the lines of:

template instantiation Foo!(int, float, float, char[]): could not 
instantiate.
Candidates are:
valhalla.odin.Foo(T : long, U, T, V[char]) : char[] is not a V[char]
valhalla.thor.Foo(T : class, U...) : int is not a class
...



More information about the Digitalmars-d mailing list