简体   繁体   English

Visual C ++ 2013中的C ++ 11 std :: enable_if重载

[英]C++ 11 std::enable_if overloading in Visual C++ 2013

The following code snippet was taken from folly , checking whether one integer is greater than the other in 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 show me the expected compiling result, but Visual C++ 2013 gives me an error at the second template function greater_than_impl : GCC 4.8.2向我展示了预期的编译结果,但Visual C ++ 2013在第二个模板函数greater_than_impl给出了一个错误:

C2995: function template has already been defined C2995:功能模板已定义

seems that std::enable_if overloading was not recognized, is Visual C++ 2013 lack of any SFINAE feature? 似乎std :: enable_if重载未被识别,Visual C ++ 2013是否缺少任何SFINAE功能?

VC++ 2013 doesn't support constexpr. VC ++ 2013不支持constexpr。 And from what I can tell they implement max and min as non-const static functions. 从我可以告诉他们将maxmin实现为非const static函数。 You can't use them where constant expressions are required, ie: 您不能在需要常量表达式的地方使用它们,即:

#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

As a workaround, I've tried to copy libstdc++'s implementation of numeric_limits , something like the following: 作为一种解决方法,我试图复制libstdc ++的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;
    }
};

Unfortunately, this gives the same function template redefinition error. 不幸的是,这给出了相同的函数模板重定义错误。 You can use INT_MAX and INT_MIN directly, but that would require specializing for 16 types (in libstdc++'s case.) It's better to probably avoid this approach altogether and follow Praetorian's advice. 您可以直接使用INT_MAXINT_MIN ,但这需要专门针对16种类型(在libstdc ++的情况下)。最好完全避免这种方法并遵循Praetorian的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM