以下代码:
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中的成员函数,但可以接受成员常量。您可以使用以下简单的变通办法,并使用struct
进行附加级别的间接寻址: