Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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_empty template in C++
In this article we will be discussing the working, syntax and examples of std::is_empty template in C++ STL.
is_empty is a template which comes under the <type_traits> header file. This template is used to check whether the given class T is an empty class or not.
What is an empty class?
A class is known as empty, when there is no data stored in a class. Empty class satisfies the following −
- Must have no non-static members other than bit fields of length 0.
- Must have no virtual base class or virtual functions.
- Must have no base class.
Syntax
template <class T>is_empty;
Parameters
The template can have only parameter of class T, and check whether class T is an empty class or not.
Return value
It returns a Boolean value, true if the given type is an empty class, and false if the given type is not an empty class.
Example
Input: class A{};
is_empty<A>::value;
Output: true
Input: class B{ void fun() {} };
is_empty<B>::value;
Output: true
Example
#include <iostream>
#include <type_traits>
using namespace std;
class TP_1 {
};
class TP_2 {
int var;
};
class TP_3 {
static int var;
};
class TP_4 {
~TP_4();
};
int main() {
cout << boolalpha;
cout << "checking for is_empty template for a class with no variable: "<< is_empty<TP_1>::value;
cout <<"\nchecking for is_empty template for a class with one variable: "<< is_empty<TP_2>::value;
cout <<"\nchecking for is_empty template for a class with one static variable: "<< is_empty<TP_3>::value;
cout <<"\nchecking for is_empty template for a class with constructor: "<< is_empty<TP_4>::value;
return 0;
}
Output
If we run the above code it will generate the following output −
checking for is_empty template for a class with no variable: true checking for is_empty template for a class with one variable: false checking for is_empty template for a class with one static variable: true checking for is_empty template for a class with constructor: true
Example
#include <iostream>
#include <type_traits>
using namespace std;
struct TP_1 {
};
struct TP_2 {
int var;
};
struct TP_3 {
static int var;
};
struct TP_4 {
~TP_4();
};
int main() {
cout << boolalpha;
cout << "checking for is_empty template for a structure with no variable: "<< is_empty<TP_1>::value;
cout <<"\nchecking for is_empty template for a structure with one variable: "<< is_empty<TP_2>::value;
cout <<"\nchecking for is_empty template for a structure with one static variable: "<< is_empty<TP_3>::value;
cout <<"\nchecking for is_empty template for a structure with constructor: "<< is_empty<TP_4>::value;
return 0;
}
Output
If we run the above code it will generate the following output −
checking for is_empty template for a structure with no variable: true checking for is_empty template for a structure with one variable: false checking for is_empty template for a structure with one static variable: true checking for is_empty template for a structure with constructor: true
Advertisements