is_pod template in C++

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of std::is_pod template in C++ STL.

is_ pod is a template which comes under <type_traits> header file. This template is used to check whether the given type T is a POD(plain-old-data) type or not.

What is POD(Plain old data)?

Plain old data(POD) types are those types which are also in old C language. POD types also include scalar types. POD class type is that class type which is both, trivial (which can be initialised statically) and standard layout (which is a simple data structure like struct and union).

Syntax

template <class T> is_pod;

Parameters

The template can have only parameter of type T, and check whether the given type is a Plain Old Data type or not.

Return value

It returns a Boolean value, true if the given type is a Plain Old Data, and false if the given type is not a Plain Old Data.

Example

Input: class final_abc{ final_abc(); };
   is_pod<final_abc>::value;
Output: False

Input: is_pod<int>::value;
Output: True

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
struct TP_1 {
   int var_1;
};
struct TP_2 {
   int var_2;
   private:
   int var_3;
};
struct TP_3 {
   virtual void dummy();
};
int main() {
   cout << boolalpha;
   cout << "checking for is_pod:";
   cout << "\nTP_1: " << is_pod<TP_1>::value;
   cout << "\nTP_2: " << is_pod<TP_2>::value;
   cout << "\nTP_3: " << is_pod<TP_3>::value;
   return 0;
}

Output

If we run the above code it will generate the following output −

checking for is_pod:
TP_1: true
TP_2: false
TP_3: false

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
class TP_1 {
   int var_1;
};
class TP_2 {
   int var_2;
   private:
   int var_3;
};
class TP_3 {
   virtual void dummy();
};
int main() {
   cout << boolalpha;
   cout << "checking for is_pod:";
   cout << "\nTP_1: " << is_pod<TP_1>::value;
   cout << "\nTP_2: " << is_pod<TP_2>::value;
   cout << "\nTP_3: " << is_pod<TP_3>::value;
   return 0;
}

Output

If we run the above code it will generate the following output −

checking for is_pod:
TP_1: true
TP_2: true
TP_3: false
raja
Published on 23-Mar-2020 05:34:01
Advertisements