gpt4 book ai didi

c++ - 在 Visual Studio 2012 中限制函数模板类型

转载 作者:太空狗 更新时间:2023-10-29 21:23:36 24 4
gpt4 key购买 nike

Visual Studio 2012 中有什么方法可以将函数模板限制为特定类型吗?

这个在 GCC 中有效,但 MSVC 生成error C4519: default template arguments are only allowed on a class template

#include <type_traits>

template <class float_t, class = typename std::enable_if< std::is_floating_point<float_t>::value >::type>
inline float_t floor(float_t x)
{
float_t result;
//...
return result;
}

交叉编译器解决方案将是最好的。还有其他选择吗?

最佳答案

通常,你会这样写

template <class float_t>
typename std::enable_if< std::is_floating_point<float_t>::value, float_t>::type
floor(float_x x) {...}

这就是 enable_if 的用途。

关于c++ - 在 Visual Studio 2012 中限制函数模板类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17903611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com