Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;11 std::在Visual C+中重载时启用"U"+;2013_C++_C++11_Sfinae - Fatal编程技术网

C++ C++;11 std::在Visual C+中重载时启用"U"+;2013

C++ C++;11 std::在Visual C+中重载时启用"U"+;2013,c++,c++11,sfinae,C++,C++11,Sfinae,以下代码片段取自,用于检查编译时一个整数是否大于另一个整数 #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()

以下代码片段取自,用于检查编译时一个整数是否大于另一个整数

#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;
}
#包括
#包括
模板
bool大于impl(
typename std::启用\u如果<
(rhs=std::数值限制::min()),
LHS
>::类型const lhs
) {
返回左侧>右侧;
}
模板
bool大于impl(
typename std::启用\u如果<
(rhs>std::数值限制::max()),
LHS
>::type const
) {
返回false;
}
模板
bool大于impl(
typename std::启用\u如果<
(rhs::type const
) {
返回true;
}
模板
bool大于(左侧常量左侧){
返回大于实际值的值<
RHS,RHS,typename std::remove_reference::type
>(lhs);
}
int测试()
{
自动v=大于(0);
std::cout不支持constexpr。根据我所知,它是非const
静态
函数。您不能在需要常量表达式的地方使用它们,例如:

#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

不幸的是,这会导致相同的函数模板重新定义错误。您可以直接使用
INT_MAX
INT_MIN
,但这需要专门化16种类型(对于libstdc++)最好完全避免使用这种方法,并遵循建议。

在问题中包含有关错误的附加信息将非常有用。错误出现在哪行?它给出的确切错误文本是什么?我只能找到相关的错误报告:这需要
constexpr
用于
数值\u limits::max()和
最小值()
,VS2013没有。如果你安装最新的CTP,它可能会起作用,我相信这会增加一些
constexpr
支持,但即使这样,他们也不太可能更新stdlib。但我不明白在任何情况下都需要这样做;如果你有
constexpr
支持,为什么不直接实现它呢?另外,C++14还增加了
constexpr
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;
    }
};