C ++ 11 std :: enable_if overload in Visual C ++ 2013

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?

+3
source share
1 answer

V++ 2013 constexpr. , , max min non-const static. , , ..:

#include <limits>
#include <array>

int main()
{
    std::array<int, std::numeric_limits<int>::max()> a;
}

error C2975: '_Size' : invalid template argument for 'std::array', expected compile-time constant expression

, libstd++ numeric_limits, - :

struct wrapper_base
{
  // member variables
};

template <typename T>
struct wrapper : public wrapper_base
{
    static const T max()
    {
        return T();
    }

    static const T min()
    {
        return T();
    }
};

template <>
    struct wrapper<int>
{
    static const int max()
    {
        return INT_MAX;
    }

    static const int min()
    {
        return INT_MIN;
    }
};

, . INT_MAX INT_MIN, 16 ( libstd++). , , Praetorian's > .

+2

All Articles