is_empty template in C++

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::is_empty template
  
#include <iostream>
#include <type_traits>
using namespace std;
  
// empty struct
struct GFG1 {
};
  
// struct with local variable
struct GFG2 {
    int variab;
};
  
// Struct with global variable
struct GFG3 {
    static int variab;
};
  
// Struct with virtual destructor
struct GFG4 {
    virtual ~GFG4();
};
  
// Driver code
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;
}

chevron_right


Output:

Is GFG1 empty: true
Is GFG2 empty: false
Is GFG3 empty: true
Is GFG4 empty: false

Program 2: Using classes

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::is_empty template
  
#include <iostream>
#include <type_traits>
using namespace std;
  
// empty class
class GFG1 {
};
  
// class with local variable
class GFG2 {
    int variab;
};
  
// class with global variable
class GFG3 {
    static int variab;
};
  
// class with virtual destructor
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;
}

chevron_right


Output:

Is GFG1 empty: true
Is GFG2 empty: false
Is GFG3 empty: true
Is GFG4 empty: false


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.