在Visual Studio 2012中限制功能模板类型

时间:2013-07-27 23:48:56

标签: c++ visual-c++ visual-studio-2012 c++11

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;
}

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

1 个答案:

答案 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的用途。