The std::is_empty template of C++ STL is used to check whether the given type is empty or not. This method returns a boolean value showing whether the given type is empty or not.
Syntax:
template < class T > struct is_empty;
Parameters: This template contains single parameter T (Trait class) that identifies whether T is a empty type.
Return Value: This template returns a boolean value as shown below:
- True: if the type is a empty.
- False: if the type is not empty.
Below programs illustrate the std::is_empty template in C++ STL:
Program 1: Using Structs
#include <iostream>
#include <type_traits>
using namespace std;
struct GFG1 {
};
struct GFG2 {
int variab;
};
struct GFG3 {
static int variab;
};
struct GFG4 {
virtual ~GFG4();
};
int main()
{
cout << boolalpha;
cout << "Is GFG1 empty: "
<< is_empty<GFG1>::value
<< '\n' ;
cout << "Is GFG2 empty: "
<< is_empty<GFG2>::value
<< '\n' ;
cout << "Is GFG3 empty: "
<< is_empty<GFG3>::value
<< '\n' ;
cout << "Is GFG4 empty: "
<< is_empty<GFG4>::value
<< '\n' ;
return 0;
}
|
Output:
Is GFG1 empty: true
Is GFG2 empty: false
Is GFG3 empty: true
Is GFG4 empty: false
Program 2: Using classes
#include <iostream>
#include <type_traits>
using namespace std;
class GFG1 {
};
class GFG2 {
int variab;
};
class GFG3 {
static int variab;
};
class GFG4 {
virtual ~GFG4();
};
int main()
{
cout << boolalpha;
cout << "Is GFG1 empty: "
<< is_empty<GFG1>::value
<< '\n' ;
cout << "Is GFG2 empty: "
<< is_empty<GFG2>::value
<< '\n' ;
cout << "Is GFG3 empty: "
<< is_empty<GFG3>::value
<< '\n' ;
cout << "Is GFG4 empty: "
<< is_empty<GFG4>::value
<< '\n' ;
return 0;
}
|
Output:
Is GFG1 empty: true
Is GFG2 empty: false
Is GFG3 empty: true
Is GFG4 empty: false
Last Updated :
19 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...