{"id":5054,"date":"2016-12-03T08:09:39","date_gmt":"2016-12-03T08:09:39","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/generalized-plain-old-data\/"},"modified":"2023-06-26T12:34:16","modified_gmt":"2023-06-26T12:34:16","slug":"generalized-plain-old-data","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/generalized-plain-old-data\/","title":{"rendered":"Generalized Plain Old Data"},"content":{"rendered":"<p><strong>P<\/strong>lain <strong>O<\/strong>ld<strong> D<\/strong>ata (POD) obeys the C standard layout. Therefore, you can directly apply the <a href=\"https:\/\/www.modernescpp.com\/index.php\/type-traits-performance-matters\">fast C functions<\/a> <span style=\"font-family: courier new,courier;\">memcopy, <\/span><span style=\"font-family: courier new,courier;\">memmove<\/span>, <span style=\"font-family: courier new,courier;\">memset, or <\/span><span style=\"font-family: courier new,courier;\">memcmp.<\/span><\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<h2>PODs<\/h2>\n<p>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.&nbsp;<\/p>\n<p>Which requirements hold for the C++11 class to be a POD? <strong>A class is a POD, if it&#8217;s trivial, has a standard layout, and all of its non-static members are PODs.<\/strong> The definition is quite concise. But what does it mean that class should be trivial and has a standard layout?<\/p>\n<p>Now the standard reads like German legal text.<\/p>\n<h3>Trivial class<\/h3>\n<p>A class is trivial if it&nbsp;&nbsp;<\/p>\n<ul>\n<li>has a trivial default constructor.<\/li>\n<li>is trivially copyable.\n<p>&nbsp;<\/p>\n<\/li>\n<\/ul>\n<p>A trivially copyable class is a class that<\/p>\n<ul>\n<li>has no non-trivial copy or move constructor.<\/li>\n<li>has no non-trivial copy or move assignment operator.<\/li>\n<li>has a trivial destructor.<\/li>\n<\/ul>\n<p>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.<\/p>\n<p>The definition of a POD goes on with the standard layout.<\/p>\n<h3>Standard layout<\/h3>\n<p>A class has a standard layout if it has no<\/p>\n<ul>\n<li>virtual functions.<\/li>\n<li>virtual base classes.<\/li>\n<li>references.<\/li>\n<li>different access specifiers (<span style=\"font-family: courier new,courier;\">public, protected<\/span><span style=\"font-family: courier new,courier;\">, a<\/span>nd <span style=\"font-family: courier new,courier;\">private<\/span>).<\/li>\n<\/ul>\n<p>It&#8217;s a lot easier to check with the&nbsp;help of the<a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/type-traits\"> type-traits library<\/a> if the class is POD.<\/p>\n<\/p>\n<h3>Checking types with the type-traits library<\/h3>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ pod.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;type_traits&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">struct<\/span> Pod{\r\n  <span style=\"color: #2b91af;\">int<\/span> a;\r\n};\r\n\r\n<span style=\"color: #0000ff;\">struct<\/span> NotPod{\r\n    <span style=\"color: #2b91af;\">int<\/span> i;\r\n  private:\r\n    <span style=\"color: #2b91af;\">int<\/span> j;\r\n};\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  std::cout &lt;&lt; std::boolalpha &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_pod&lt;Pod&gt;::value: \"<\/span> &lt;&lt; std::is_pod&lt;Pod&gt;::value &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_pod&lt;NotPod&gt;::value: \"<\/span> &lt;&lt; std::is_pod&lt;NotPod&gt;::value &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n   \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_trivial&lt;NotPod&gt;::value: \"<\/span> &lt;&lt; std::is_trivial&lt;NotPod&gt;::value &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::is_standard_layout&lt;NotPod&gt;::value: \"<\/span> &lt;&lt; std::is_standard_layout&lt;NotPod&gt;::value &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n  \r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The class <span style=\"font-family: courier new,courier;\">Pod<\/span> in lines 6 &#8211; 8 is a POD, but not the class <span style=\"font-family: courier new,courier;\">NotPod<\/span> (lines 10 -15). We get the answer relatively easy with the help of the function <span style=\"font-family: courier new,courier;\">std::is_pod<\/span> (lines 21 &#8211; 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 <span style=\"font-family: courier new,courier;\">NotPod<\/span> even more. The result is: <span style=\"font-family: courier new,courier;\">NotPod<\/span> is trivial but has no standard layout. <span style=\"font-family: courier new,courier;\">NotPod<\/span> has no standard layout because the variable <span style=\"font-family: courier new,courier;\">i<\/span> is public. On the contrary, the variable <span style=\"font-family: courier new,courier;\">j <\/span><span style=\"font-family: courier new,courier;\">i<\/span>s private.<\/p>\n<p>The output of the program depicts the explanation.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5053\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/POD.PNG\" alt=\"POD\" width=\"800\" height=\"255\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/POD.PNG 858w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/POD-300x96.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/POD-768x245.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>This post finishes the series of posts about the features in C++ that are very important from the performance perspective. In the <a href=\"https:\/\/www.modernescpp.com\/index.php\/careful-handling-of-resources\">next post<\/a><span id=\"transmark\">, <\/span>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 <span style=\"font-family: courier new,courier;\">std::shared_ptr, std::unique_ptr,<\/span> and<span style=\"font-family: courier new,courier;\"> std::weak_ptr, <\/span>and the manual memory management with <span style=\"font-family: courier new,courier;\">new<\/span> becomes almost unnecessary.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Plain Old Data (POD) obeys the C standard layout. Therefore, you can directly apply the fast C functions memcopy, memmove, memset, or memcmp.<\/p>\n","protected":false},"author":21,"featured_media":5053,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[512],"class_list":["post-5054","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-pod"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5054"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5054\/revisions"}],"predecessor-version":[{"id":6919,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5054\/revisions\/6919"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5053"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}