C++如何识别无符号类型?

Avi*_*ush 2 c++ templates

假设我想为每种类型写一个绝对值函数.就像是:

template <class T>
T MyAbsVersion(T num)
{
    return (num > 0) ? num : num*-1;
}
Run Code Online (Sandbox Code Playgroud)

但是,我想拒绝无符号类型的数字.有什么好方法吗?

谢谢

P0W*_*P0W 7

使用std::is_signed只接受数signed类型

template<class T ,
          typename std::enable_if< std::is_signed<T>::value  >::type* = nullptr >
T myabs(T num) 
{
    return (num > 0) ? num : num*-1;
}
Run Code Online (Sandbox Code Playgroud)

演示 Here