is_abstract template in C++

C++Server Side ProgrammingProgramming

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

Is_abstract template helps to check whether the class is an abstract class or not.

What is an abstract class?

Abstract class is a class which has at least one Pure Virtual Function. We use Abstract classes because when we define the function, we don’t know its implementation, so it is very helpful in the case when we don't know what the function of a class is supposed to do further but we are sure that there must be a function like this in our system. So, we declare a pure virtual function which is only declared and don’t have the implementation of that function.

So, when we want to check from the instance of the class whether the class is an Abstract class or not, we use is_abstract().

is_abstract() is inherited from the itegral_constant and gives true_type or false_type, depending whether the instance of class T is polymorphic class type or not.

Syntax

template <class T> struct is_abstract;

Parameters

This template can have only one parameter T, that is of class type to check whether the class T is an abstract class or not.

Return value

This function returns bool type value, true or false.

It returns true if T is an abstract class, and false if T is not an abstract class.

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
struct TP_1 {
   int var;
};
struct TP_2 {
   virtual void dummy() = 0;
};
class TP_3 : TP_2 {
};
int main() {
   cout << boolalpha;
   cout << "checking for is_abstract: ";
   cout << "\nstructure TP_1 with one variable :"<< is_abstract<TP_1>::value;
   cout << "\nstructure TP_2 with one virtual variable : "<< is_abstract<TP_2>::value;
   cout << "\nclass TP_3 which is derived from TP_2 structure : "<< is_abstract<TP_3>::value;
   return 0;
}

Output

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

checking for is_abstract:
structure TP_1 with one variable : false
structure TP_2 with one virtual variable : true
class TP_3 which is derived from TP_2 structure : true

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
struct TP_1 {
   virtual void dummy() = 0;
};
class TP_2 {
   virtual void dummy() = 0;
};
struct TP_3 : TP_2 {
};
int main() {
   cout << boolalpha;
   cout << "checking for is_abstract: ";
   cout << "\nstructure TP_1 with one virtual function :"<< is_abstract<TP_1>::value;
   cout << "\nclass TP_2 with one virtual function : "<< is_abstract<TP_2>::value;
   cout << "\nstructure TP_3 which is derived from TP_2 class : "<< is_abstract<TP_3>::value;
   return 0;
}

Output

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

checking for is_abstract:
structure TP_1 with one virtual function : true
class TP_2 with one virtual function : true
structure TP_3 which is derived from TP_2 class : true
raja
Published on 23-Mar-2020 07:23:30
Advertisements