以下代码:
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
(or 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;
}