Search code examples
c++template-meta-programmingsfinaeenable-if

Why does this substitution failure create an error?


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?


Solution

  • 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.