is_fundamental Template in C++

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of std::is_fundamental template in C++ STL.

is_ fundamental is a template which comes under the <type_traits> header file. This template is used to check whether the given type T is a fundamental data type or not.

What is Fundamental type?

Fundamental types are the built-in types which are already declared in the compiler itself. Like int, float, char, double, etc. These are also known as built-in data types.

All the data types which are user-defined like: class, enum, struct, references or pointers, are not part of the fundamental type.

Syntax

template <class T> is_fundamental;

Parameters

The template can have only parameter of type T, and check whether the given type is a final class type or not.

Return value

It returns a Boolean value, true if the given type is a fundamental data type, and false if the given type is not a fundamental data type.

Example

Input: class final_abc;
   is_fundamental<final_abc>::value;
Output: False

Input: is_fundamental<int>::value;
Output: True

Input: is_fundamental<int*>::value;
Output: False

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
class TP {
   //TP Body
};
int main() {
   cout << boolalpha;
   cout << "checking for is_fundamental:";
   cout << "\nTP: "<< is_fundamental<TP>::value;
   cout << "\nchar :"<< is_fundamental<char>::value;
   cout << "\nchar& :"<< is_fundamental<char&>::value;
   cout << "\nchar* :"<< is_fundamental<char*>::value;
   return 0;
}

Output

If we run the above code it will generate the following output −

checking for is_fundamental:
TP: false
char : true
char& : false
char* : false

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
int main() {
   cout << boolalpha;
   cout << "checking for is_fundamental:";
   cout << "\nint: "<< is_fundamental<int>::value;
   cout << "\ndouble :"<< is_fundamental<double>::value;
   cout << "\nint& :"<< is_fundamental<int&>::value;
   cout << "\nint* :"<< is_fundamental<int*>::value;
   cout << "\ndouble& :"<< is_fundamental<double&>::value;
   cout << "\ndouble* :"<< is_fundamental<double*>::value;
   return 0;
}

Output

If we run the above code it will generate the following output −

checking for is_fundamental:
int: true
double : true
int& : false
int* : false
double& : false
double* : false
raja
Published on 23-Mar-2020 05:27:15
Advertisements