Search code examples
c++c++14enable-if

strange behavior of enable_if


does anyone know why the following code compiles

static const size_t CONSTANT = /* ... */;

template< size_t M = CONSTANT, typename std::enable_if_t< M!=1, size_t > = 0 >
res_type</*...*/> foo()
{
  // ...
}

while this does not:

static const size_t CONSTANT = /* ... */;

template< typename std::enable_if_t< CONSTANT!=1, size_t > = 0 >
res_type</*...*/> foo()
{
  // ...
}

Many thanks in advance.

Best


Solution

  • SFINAE requires the failed substitution to be dependant on a template parameter.

    If the substitution failure happens at the first phase of the lookup (in other words, when it is not dependant on template parameters) the program is ill-formed, no diagnostics required. But the popular compilers yield a readable error in this case.

    Otherwise the compiler must wait for the instantiation of the template specialization to know whether substitution can take place. If it cannot, it is required by the language not to produce a hard error, but be silently ignored instead.