"SFINAE is Evil"

Russell Lewis webmaster at villagersonline.com
Fri Mar 21 17:14:23 PDT 2008


Jason House wrote:
> SFINAE - Substitution failure is not an error
> 
> This post is all about templates and their static if counterparts.  With the
> enhanced expressiveness of D, is there a need for SFINAE from C++?

Here's a classic example of how SFINAE makes things hard.  Look at the 
code below.  You'd expect the static assert to fail, right?  Give you an 
error message at the right location, and point you towards your bug?

Nope.

BEGIN CODE
   import std.stdio;

   template foo(TPL...)
   {
     static assert(false);
   }

   void main()
   {
     int i;
     foo!(i)();
   }
END CODE

Here's what the compiler actually says:
   sfinae.d(3): template sfinae.foo(TPL...) is not a function template
   sfinae.d(11): template sfinae.foo(TPL...) cannot deduce template 
function from argument types !(i)()

Not even a mention of the real problem.  Now imagine that you are using 
non-trivial templates!

Russ



More information about the Digitalmars-d mailing list