Generalized Plain Old Data

Contents[Show]

Plain Old Data (POD) obeys the C standard layout. Therefore, you directly can apply the fast C functions memcopy, memmove, memset, or memcmp.

 

PODs

PODs are in classical C++ fundamental types like booleans, integers of floating point number. The restriction will not hold for C++11. With C++11 even classes and structs can be PODs. For simplicity reasons I only speak about classes. 

Which requirements holds for the C++11 class to be a POD? A class is a POD, if it's trivial, has a standard layout and all of its non-static members are PODs. The definition is quite concise. But what does it mean that class should be trivial and has standard layout?

Now the standard reads like German legal text.

Trivial class

A class is trivial, if it  

  • has a trivial default constructor.
  • is trivial copyable.

     

A trivial copyable class is a class that

  • has no non-trivial copy or move constructor.
  • has no non-trivial copy or move assignment operator.
  • has a trivial destructor.

Non-trivial means that the mentioned methods are implemented by the developer. If a method is requested from the compiler via the keyword default or automatically generated from the compiler, the method is trivial.

The definition of a POD goes on with the standard layout.

Standard layout

A class has a standard layout if it has no

  • virtual functions.
  • virtual base classes.
  • references.
  • different access specifier (public, protected, and private).

It's a lot easier to check with the help of the type-traits library if the class is POD.

Checking types with the type-traits library

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// pod.cpp

#include <iostream>
#include <type_traits>

struct Pod{
  int a;
};

struct NotPod{
    int i;
  private:
    int j;
};

int main(){

  std::cout << std::boolalpha << std::endl;
  
  std::cout << "std::is_pod<Pod>::value: " << std::is_pod<Pod>::value << std::endl;
  std::cout << "std::is_pod<NotPod>::value: " << std::is_pod<NotPod>::value << std::endl;
  
  std::cout << std::endl;
   
  std::cout << "std::is_trivial<NotPod>::value: " << std::is_trivial<NotPod>::value << std::endl;
  std::cout << "std::is_standard_layout<NotPod>::value: " << std::is_standard_layout<NotPod>::value << std::endl;
  
  std::cout << std::endl;
  
}

 

The class Pod in the lines 6 - 8 is a POD, but not the class NotPod (lines 10 -15). We get the answer quite easy with the help of the function std::is_pod (line 21 - 22) from the type-traits library. But we can do even better with the type-traits library. I analyse in the line 26 and 27 the class NotPod even more. The result is: NotPod is trivial, but has no standard layout. NotPod has no standard layout because the variable i is public. In contrary the variable j is private.

The output of the program depicts the explanation.

POD

What's next?

This post finishes the series of posts about the features in C++ that are very important from the performance perspective. In the next post I will continue my blog with posts about the careful handling of resources. Memory management has a high priority in the embedded development. Therefore, it fits very well that C++11 has the new smart pointers std::shared_ptr, std::unique_ptr, and std::weak_ptr and the manual memory management with new becomes almost unnecessary.

 

 

 

 

Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner,  Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, Sudhakar Balagurusamy, lennonli, and Pramod Tikare Muralidhara.

 

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, and Dendi Suhubdy

 

Seminars

I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable (Online)

Deutsch

English

Standard Seminars 

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

New

Contact Me

Modernes C++,

RainerGrimmSmall

 

Tags: enum

Comments   

0 #1 Marco 2016-12-04 18:04
Hi, just an observation. Technically, the only requirement for an object to be compatible with C layout is to be standard layout.

However, both trivially copyable and standard layout objects can be memcpy'd (and others).
Quote

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 2568

Yesterday 7515

Week 34179

Month 192610

All 5061924

Currently are 177 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments