- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
is_polymorphic template in C++
In this article we will be discussing the working, syntax and examples of std::is_polymorphic template in C++ STL.
is_polymorphic is a template that comes under the <type_traits> header file in C++. This template is used to check whether the class is a polymorphic class or not and return the result either true or false accordingly.
What is a polymorphic class?
A class which declares the virtual function from an abstract class where the virtual function is declared. This class have the declaration of the virtual function declared in other class.
Syntax
template <class T> is_polymorphic;
Parameters
The template can have only parameter of type T, and check whether the given type is a polymorphic class or not.
Return value
It returns a Boolean value, true if the given type is a polymorphic class, and false if the given type is not a polymorphic class.
Example
Input: class B { virtual void fn(){} }; class C : B {}; is_polymorphic<B>::value; Output: True Input: class A {}; is_polymorphic<A>::value; Output: False
Example
#include <iostream> #include <type_traits> using namespace std; struct TP { virtual void display(); }; struct TP_2 : TP { }; class TP_3 { virtual void display() = 0; }; struct TP_4 : TP_3 { }; int main() { cout << boolalpha; cout << "Checking for is_polymorphic: "; cout << "\n structure TP with one virtual function : "<<is_polymorphic<TP>::value; cout << "\n structure TP_2 inherited from TP: "<<is_polymorphic<TP_2>::value; cout << "\n class TP_3 with one virtual function: "<<is_polymorphic<TP_3>::value; cout << "\n class TP_4 inherited from TP_3: "<< is_polymorphic<TP_4>::value; return 0; }
Output
If we run the above code it will generate the following output −
Checking for is_polymorphic: structure TP with one virtual function : true structure TP_2 inherited from TP: true class TP_3 with one virtual function: true class TP_4 inherited from TP_3: true
Example
#include <iostream> #include <type_traits> using namespace std; struct TP { int var; }; struct TP_2 { virtual void display(); }; class TP_3: TP_2 { }; int main() { cout << boolalpha; cout << "Checking for is_polymorphic: "; cout << "\n structure TP with one variable : "<<is_polymorphic<TP>::value; cout << "\n structure TP_2 with one virtual function : "<<is_polymorphic<TP_2>::value; cout << "\n class TP_3 inherited from structure TP_2: "<<is_polymorphic<TP_3>::value; return 0; }
Output
If we run the above code it will generate the following output −
Checking for is_polymorphic: structure TP with one variable : false structure TP_2 with one virtual function : true class TP_3 inherited from structure TP_2 : true