Why does this substitution failure create an error?

Viewed 76

In a template specialization I have a template argument with an enable_if with parameter that results in the enable_if not having a 'type' member, and so that template specialization should fail, but not create an error:

#include <type_traits>


template <typename value_t_arg, typename T = void>
struct underlyingtype
{
    using underlyingtype_t = value_t_arg;
};

template <typename value_t_arg>
struct underlyingtype < value_t_arg, typename std::enable_if<false>::type>
// std::enable_if<false> has no 'type' member, and so substitution should fail, 
// but no create an error, right?
{
    //using underlyingtype_t = value_t_arg::integral_t;
};

Why is there an error created here?

2 Answers

Your code is ill-formed (no diagnostic required) because the condition is always false regardless of the template argument, meaning the specialization would be ill-formed for every possible template argument.

[temp.res.general]/6.1

The program is ill-formed, no diagnostic required, if:

— no valid specialization can be generated for a template ... and the template is not instantiated, ...

Partial specializations appear to count as "templates" for the purposes of this section.

It's SFINAE, not FINAE. For the compiler to be happy with std::enable_if<false>::type you'd need the latter, which would mean something nonsensical.

Related