mercredi 2 mars 2016

std::underlying_type : Does SFINAE prevent undefined behavior?

std::underlying_type invokes undefined behavior when used with a non enum type. But where does the undefined behavior appears?

In this code :

template<typename E>
constexpr std::enable_if_t<std::is_enum<E>::value, std::underlying_type_t<E>> IntEnum(E e)
{
    return static_cast<std::underlying_type_t<E>>(e);
}

I tried to use std::enable_if to prevent the user from calling IntEnum with non-enum types. But as enable_if is evaluated before deciding if the function can be called, its template arguments get evaluated too, including std::underlying_type_t<E>. So is this UB if called with a non-enum type? How can i change it?

Aucun commentaire:

Enregistrer un commentaire