Featured image of post C++ POD type

C++ POD type

POD is an acronym for **Plain Old Data** in C++, referring to a data type that can be copied and transferred via simple memory copy

Preface

Sometimes, when reading other people’s articles, I would see the word POD, but I was never quite sure what it meant, until I came across it again today, so I looked into what POD is.

Main article

POD is an acronym for Plain Old Data in C++, referring to a data type that can be copied and transferred via simple memory copy. This means that the type is compatible with types used for the C programming language, i.e. it can interact directly with the C library in binary form.

Characteristics of the POD type include:

  1. Scalar type
  2. No dummy functions: Classes or structures of type POD do not contain dummy functions, i.e. they do not use the polymorphic features of C++.
  3. No Virtual Inheritance: Classes or structures of type POD do not involve virtual inheritance, which means that their inheritance relationship is direct and explicit.
  4. C-compatible: Objects of type POD can interact directly with C code because they follow C’s structure layout rules.
  5. Triviality: POD types need to satisfy triviality, i.e., their default constructor, copy constructor, destructor, and assignment operator are all trivial (no extra behavior).
  6. Standard Layout: POD types also need to satisfy standard layout requirements, which means that the non-static members of the type are arranged in the order in which they appear in the class declaration, and that there are no gaps between class members.

We can check if a type is a POD type using the metaprogramming std::is_pod_v:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include <type_traits>
 
struct A { int m; };
static_assert(std::is_pod_v<A> == true);
 
class B: public A { int m; };
static_assert(std::is_pod_v<B> == false);
 
struct C { virtual void foo(); };
static_assert(std::is_pod_v<C> == false);

Note: This type requirement has been deprecated in the C++ standard. All uses of it have been replaced with more discretionary type requirements such as TrivialType. (C++20 onwards)

In C++20 onwards, we can use std::is_trivial_v to check if a type is trivial:

1
2
3
4
5
6
7
8
9
#include <type_traits>
 
struct A { int m; };
static_assert(std::is_trivial_v<A> == true);
 
struct B { B() {} };
static_assert(std::is_trivial_v<B> == false);
 
int main() {}

Summary

Simply put, a POD type is a type that is compatible with the C language. Since C++20, PODs have been replaced by the more studied Trivial Types. In general, understanding POD types is helpful when it comes to low-level programming, memory management, and interacting with the C interface, which is generally not the case.

POD Type

Trivial class

POD class

std::is_pod

Licensed under CC BY-NC-SA 4.0