在 MSVC2017 上使用 numeric_limits<T>::max() 的 SFINAE

SFINAE with numeric_limits<T>::max() on MSVC2017

以下代码:

template <typename T, typename U>
typename std::enable_if<
    std::numeric_limits<T>::max() == std::numeric_limits<U>::max(),
    bool>::type
same_max() {
    return true;
}

template <typename T, typename U>
typename std::enable_if<
    std::numeric_limits<T>::max() != std::numeric_limits<U>::max(),
    bool>::type
same_max() {
    return false;
}

无法在 MSVC2017 上编译(在 gcc/clang 上正常),出现以下错误:

error C2995: 'std::enable_if<,bool>::type same_max(void)': function template has already been defined

这是我的 SFINAE 的问题,还是 MSVC 中的错误?

注意:使用 std::numeric_limits<T>::is_signed (或 std::is_signed<T>::value )而不是 std::numeric_limits<T>::max() 可以很好地编译:

template <typename T, typename U>
typename std::enable_if<
    std::is_signed<T>::value == std::is_signed<U>::value,
    bool>::type
same_signedness() {
    return true;
}

template <typename T, typename U>
typename std::enable_if<
    std::is_signed<T>::value != std::is_signed<U>::value,
    bool>::type
same_signedness() {
    return false;
}

这看起来绝对像是编译器中的错误。它不接受 SFINAE 中的成员函数(请注意,问题中的代码不仅对 min()/max() 失败,而且对 epsilon()lowest() 等任何成员函数都失败), 但它确实接受成员常量。您可以使用以下简单的解决方法,并使用 struct(如果限于 C++11)的附加间接级别:

template<typename T>
struct get_max {
    static constexpr T value = std::numeric_limits<T>::max();
};

template <typename T, typename U>
typename std::enable_if<get_max<T>::value == get_max<U>::value, bool>::type
same_max() {
    return true;
}

template <typename T, typename U>
typename std::enable_if<get_max<T>::value != get_max<U>::value, bool>::type
same_max() {
    return false;
}

或变量模板 (C++14 起):

template<typename T>
inline constexpr T get_max_v = std::numeric_limits<T>::max();

template <typename T, typename U>
std::enable_if_t<get_max_v<T> == get_max_v<U>, bool>
same_max() {
    return true;
}

template <typename T, typename U>
std::enable_if_t<get_max_v<T> != get_max_v<U>, bool>
same_max() {
    return false;
}

为避免某些headers(如windef.h)中定义的min/max宏的潜在问题,函数名可以用括号括起来:

... = (std::numeric_limits<T>::max)();