Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::is_unsigned imply std::is_integral

Tags:

c++

typetraits

If I need a type that satisfy std::is_unsigned and std::is_integral, do I have to check both or only std::is_unsigned?

like image 241
FlashMcQueen Avatar asked Nov 28 '18 09:11

FlashMcQueen


2 Answers

cppreference has this line for is_unsigned (https://en.cppreference.com/w/cpp/types/is_unsigned):

this results in true for the unsigned integer types and the type bool and in false for the signed integer types and the floating-point types. For any other type, value is false.

so if is_unsigned is true, then is_integral will as well.

like image 62
Matthieu Brucher Avatar answered Oct 26 '22 12:10

Matthieu Brucher


Yes, it does according to cppreference.

Just keep in mind that this doesn't work everywhere, and is only guaranteed for native types. I had an issue with boost::multiprecision giving wrong results for integers. The best way to do this is with numeric_limits:

std::numeric_limits<MyIntType>::is_signed
like image 35
The Quantum Physicist Avatar answered Oct 26 '22 14:10

The Quantum Physicist