is_class template in C++


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

is_class template is used to check whether the defined type is class type not any other type.

What is a class?

A class is an user defined data type or a data structure which contains some data members or member functions which is declared with the keyword ‘class’.

Example

class abc {
   int data_members;
   void member_function();
};

So, is_class template checks that the type T is a class, and returns the Boolean value true or false accordingly.

Syntax

template <class T> is_class;

Parameters

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

Return value

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

Example

Input: class abc {
};
is_class<abc>::value;
Output: True

Input: union abc {
};
is_class<abc>::value;
Output: False

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
class TP_1 {
};
union TP_2 {
   int var_1;
   float var_2;
};
int main() {
   cout << boolalpha;
   cout << "checking for is_class template: ";
   cout << "\nTP_1 class : "<<is_class<TP_1>::value;
   cout << "\nTP_2 union : "<< is_class<TP_2>::value;
   return 0;
}

Output

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

checking for is_class template:
TP_1 class : true
TP_2 union : false

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
class TP_1 {
   int var_1;
   float var_2;
   char var_3;
};
union TP_2 {
   int var_1;
   float var_2;
   char var_3;
};
struct TP_3 {
   int var_1;
   float var_2;
   char var_3;
};
int main() {
   cout << boolalpha;
   cout << "checking for is_class template: ";
   cout << "\nTP_1 class : "<<is_class<TP_1>::value;
   cout << "\nTP_2 union : "<< is_class<TP_2>::value;
   cout << "\nTP_3 structure : "<< is_class<TP_3>::value;
   return 0;
}

Output

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

checking for is_class template:
TP_1 class : true
TP_2 union : false
TP_3 structure : true

Updated on: 23-Mar-2020

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements