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;
}
交叉编译器解决方案将是最好的。还有其他选择吗?
答案 0 :(得分:3)
通常情况下,您可以将其写为
template <class float_t>
typename std::enable_if< std::is_floating_point<float_t>::value, float_t>::type
floor(float_x x) {...}
这就是enable_if
的用途。