POD

Generalized Plain Old Data

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

 

PODs

PODs are in classical C++ fundamental types like booleans, integers of floating-point numbers. 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 hold 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 a standard layout?

Now the standard reads like German legal text.

Trivial class

A class is trivial if it  

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

     

A trivially 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 developer implements the mentioned methods. The method is trivial if a method is requested from the compiler via the keyword default or automatically generated from the compiler.

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

Rainer D 6 P2 500x500

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (open)
  • "Generic Programming (Templates) with C++": October 2024
  • "Embedded Programming with Modern C++": October 2024
  • "Clean Code: Best Practices for Modern C++": March 2025
  • Do you want to stay informed: Subscribe.

     

    Standard layout

    A class has a standard layout if it has no

    • virtual functions.
    • virtual base classes.
    • references.
    • different access specifiers (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 lines 6 – 8 is a POD, but not the class NotPod (lines 10 -15). We get the answer relatively easy with the help of the function std::is_pod (lines 21 – 22) from the type-traits library. But we can do even better with the type-traits library. I analyze in line 26 and 27 in 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. On the 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 carefully handling resources. Memory management has a high priority in 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, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Stephen Kelley, Kyle Dean, Tusar Palauri, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery, Matt Godbolt, and Honey Sukesan.

    Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.

    My special thanks to Embarcadero
    My special thanks to PVS-Studio
    My special thanks to Tipi.build 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS

    Modernes C++ GmbH

    Modernes C++ Mentoring (English)

    Do you want to stay informed about my mentoring programs? Subscribe Here

    Rainer Grimm
    Yalovastraße 20
    72108 Rottenburg

    Mobil: +49 176 5506 5086
    Mail: schulung@ModernesCpp.de
    Mentoring: www.ModernesCpp.org

    Modernes C++ Mentoring,

     

     

    0 replies

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

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