How to use is_empty template in C ++?

In this post, we are going to learn about a standard built-in function std::is_empty function in C++.  is_empty is a Template from STL(Standard Template Library). This type checks whether the given type is empty or not and returns the boolean value (i.e True or False).

How to use is_empty in c++?

is_empty is a template library defined in Standard Template Library. this checks whether the type associated with it is empty or not. thus returns the boolean value in the form of  True or False.
It returns True if the type is empty and False if the type is not.

C++ Code: is_empty

#include<iostream>
#include<type_traits>

using namespace std;

//Class with NO arguments. 

class check1
{
};

//Class with local variable declared inside it.

class check2
{
    int variable;
};

//Class with global variable declared inside it.
 
class check3
{
   static int variable;
};

int main()
{
cout<<boolalpha;

/*
Code followed below prints wheather the class is empty or not.
*/

cout<<"Is this class check1 empty :"<< is_empty <check1> :: value <<"\n";

cout<<"Is this class check2 empty :"<< is_empty <check2> :: value <<"\n";

cout<<"Is this class check3 empty :"<< is_empty <check3> :: value <<"\n";

return 0;
}

In the code above we have declared three-class check1,check2,check3 respectively. In the first class, there are no variables declared in this class this function obviously should return true. thus the output of the first print statement is true.

In the second class check2, there is one local variable declared inside that class but that cannot be accessed globally. thus this function returns false in the second print statement.

In the third class i.e ckeck3, there is a static variable declared inside that class thus this variable is accessed throughout the program thus this class returns the true value.

OUTPUT:

Is this class check1 empty :true
Is this class check2 empty :false
Is this class check3 empty :true

Syntax: is_empty

template < class A > strcuct is_empty;

In this syntax, A is a Trait class defined thus it only takes only one parameter, thus this function returns boolean values.

Also read: C++ isinf() function | check if given argument infinite

Leave a Reply

Your email address will not be published. Required fields are marked *