Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type traits which are true for all const/volatile/signed/unsigned version of a fundamental type

Consider the following tests:

std::is_same<T, bool>::value
std::is_same<T, char>::value
std::is_same<T, short int>::value
std::is_same<T, int>::value
std::is_same<T, long int>::value
std::is_same<T, long long int>::value
std::is_same<T, float>::value
std::is_same<T, double>::value
std::is_same<T, long double>::value

The problem is if T = const unsigned char, all tests will be false, and I would like this one std::is_same<T, char>::value to be true. Or if T = volatile signed long long int I would like std::is_same<T, long long int>::value to be true. How to do that with type_traits ?

like image 675
Vincent Avatar asked Dec 20 '22 05:12

Vincent


2 Answers

Use std::remove_cv to remove const and volatile if present:

std::is_same<std::remove_cv<T>::type, long long int>::value;
like image 173
hmjd Avatar answered Dec 24 '22 00:12

hmjd


You can use std::remove_cv to take care of the const-volatile specifiers.

And you can use std::make_signed to take care of the signed/unsigned issue. Although, I don't particularly like that idea (is unsigned char really the same as char? No.).

std::is_same< std::make_signed< std::remove_cv<T> >, char >::value;

Will be true for any of char, unsigned char, const char, const unsigned char, and the volatile versions of those.

like image 37
Mikael Persson Avatar answered Dec 24 '22 00:12

Mikael Persson