c++11 - C++ 11 enable_if的重载问题
PHP中文网
PHP中文网 2017-04-17 11:18:22
[C++讨论组]

下面这段代码取自folly,使用模板技术在编译时判断一个数字是否大于另一个,在GCC4.8.2下面编译没有问题,但是在Visual C++ 2013下面却无法通过,这是为什么?

#include <limits>
#include <type_traits>
#include <iostream>

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 main(int argc, char* argv[])
{
    auto v = greater_than<uint8_t, 0u, uint8_t>(0u);
    std::cout << v << endl;
    return 0;
}
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
大家讲道理

在stackoverflow上问了以后了解原因了,是因为VC2013还不支持const expr, numeric_limit的值无法在编译期获得。
参考这里:http://stackoverflow.com/questions/22003408/c-11-stdenable-if-overloading-in-visual-c-2013

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号