std::is_integral template in C++ with examples
Fundamental data types in C++ can be broadly divided into three categories: Integral, floating point and void type. An integral data type can be one of the following:
- int types- short, long, long long(signed and unsigned)
- char, wchar_t, char16_t, char32_t(signed and unsigned)
- bool
std::is_integral is a template that inherits from std::integral_constant and identifies whether a template is an integral data type or not. As it inherits from std::integral_constant, it has access to it’s public member constants and member functions. The member constant that tell us whether a template is an integral data type is std::is_integral::value. This member constant returns a bool value which is true if the template is an integral data type(any one of the aforementioned) and false if otherwise.
Let’s take a look at some examples and the syntax of using this template-
#include <type_traits>
using namespace std;
template <typename T>
void isintegral(T a){
cout<<boolalpha;
cout<<is_integral<T>::value<<endl;
}
int main(){
int a=1;
char b='h';
long double c=3.3333;
double d=1.23;
bool e=true;
float f=0.22;
isintegral(a);
isintegral(b);
isintegral(c);
isintegral(d);
isintegral(e);
isintegral(f);
return 0;
}Output
true true false false true false
As you can see, the output for variables a,b and e is true as they are all integral data types: int, char and bool. On the contrary, the output is false for variables c,d and f as they are all non-integral data types: long double, double and float(they come under floating point data types).
Also read:
Leave a Reply