The following code snippet was taken from folly , checking to see if one integer is larger than the other at compile time.
#include <limits>
#include <type_traits>
template <typename RHS, RHS rhs, typename LHS>
bool greater_than_impl(
typename std::enable_if<
(rhs <= std::numeric_limits<LHS>::max()
&& rhs >= std::numeric_limits<LHS>::min()),
LHS
>::type const lhs
) {
return lhs > rhs;
}
template <typename RHS, RHS rhs, typename LHS>
bool greater_than_impl(
typename std::enable_if<
(rhs > std::numeric_limits<LHS>::max()),
LHS
>::type const
) {
return false;
}
template <typename RHS, RHS rhs, typename LHS>
bool greater_than_impl(
typename std::enable_if<
(rhs < std::numeric_limits<LHS>::min()),
LHS
>::type const
) {
return true;
}
template <typename RHS, RHS rhs, typename LHS>
bool greater_than(LHS const lhs) {
return greater_than_impl<
RHS, rhs, typename std::remove_reference<LHS>::type
>(lhs);
}
int test()
{
auto v = greater_than<int, 0, int>(0);
std::cout << v << std::endl;
return 0;
}
GCC 4.8.2 will show me the expected compilation result, but Visual C ++ 2013 gives me an error in the second template function greater_than_impl:
C2995: function template already defined
It looks like the std :: enable_if overload was not recognized, is there no Visual C ++ 2013 SFINAE function?
source
share