Avi Harush
Avi Harush

Reputation: 1049

C++ how to identify unsigned types?

Let's say I want to write an absolute value function for every type. Something like:

template <class T>
T MyAbsVersion(T num)
{
    return (num > 0) ? num : num*-1;
}

However, I'd like to decline numbers of type unsigned. Any good way to do that?

Thanks

Upvotes: 2

Views: 106

Answers (1)

P0W
P0W

Reputation: 47784

Use std::is_signed to accept numbers of only signed type

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

Demo Here

Upvotes: 7

Related Questions