abraham_hilbert
abraham_hilbert

Reputation: 2271

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

Upvotes: 1

Views: 69

Answers (1)

krzaq
krzaq

Reputation: 16421

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.

Upvotes: 3

Related Questions