{"id":6507,"date":"2023-01-29T17:46:22","date_gmt":"2023-01-29T17:46:22","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/regular-type\/"},"modified":"2023-08-20T14:37:45","modified_gmt":"2023-08-20T14:37:45","slug":"regular-type","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/regular-type\/","title":{"rendered":"Regular Types"},"content":{"rendered":"<p>The concept of a regular type goes back to the creator of the Standard Template Library (STL) <a href=\"https:\/\/en.wikipedia.org\/wiki\/Alexander_Stepanov\">Alexander Stepanov<\/a>. A regular type is a user-defined type and behaves like a built-in type.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6503\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms.png\" alt=\"ClassIdioms\" width=\"650\" height=\"335\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms.png 1224w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-300x154.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-1024x527.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-768x395.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>The term concrete type is strongly related to the term regular type. Let me, therefore, start this post with the term concrete type.<\/p>\n<h2>Concrete Types<\/h2>\n<ul>\n<li>A <strong>concrete type<\/strong> is \u201cthe simplest kind of a class\u201d according to the C++ Core Guidelines. It is often called a<strong> value type<\/strong> and is not part of a type hierarchy.<\/li>\n<li>A <strong>regular type<\/strong> is a type that \u201cbehaves like an int\u201d and has, therefore, to support copy and assignment, equality, and order.<\/li>\n<\/ul>\n<p>Here are more observations from the C++ Core Guidelines:<\/p>\n<h3><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rc-concrete\">C.10: Prefer concrete types over class hierarchies<\/a><\/h3>\n<p>Use a concrete type if you do not have a use case for a class hierarchy. A concrete type is way easier to implement, smaller, and faster. You do not have to worry about inheritance, virtuality, references, or pointers, including memory allocation and deallocation. There is no virtual dispatch and, therefore, no run-time overhead.<\/p>\n<p>To make a long story short: Apply the<a href=\"https:\/\/en.wikipedia.org\/wiki\/KISS_principle\"> KISS principle<\/a> (keep it simple, stupid). Your type behaves like a value.<\/p>\n<h3><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rc-regular\">C.11: Make concrete types regular<\/a><\/h3>\n<p>Regular types (ints) are easier to understand. They are per se intuitive. This means that if you have a concrete type, consider upgrading it to a regular one. The built-in types, such as <code>int<\/code> or <code>double<\/code>, are regular but so are the containers such as <code>std::string<\/code>, <code>std::vector,<\/code> or <code>std::unordered_map<\/code>.<\/p>\n<p>&nbsp;Finally, let me write about regular types<\/p>\n<\/p>\n<h2>Regular Types<\/h2>\n<p>First of all, the concept regular types goes back to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Alexander_Stepanov\">Alexander Stepanov<\/a>, the father of the STL. He described his ideas about regular types in his paper <a href=\"http:\/\/stepanovpapers.com\/DeSt98.pdf\">&#8220;Fundamentals of Generic Programming<\/a>&#8221; and refined it in his book &#8220;<a href=\"http:\/\/elementsofprogramming.com\/\">Elements of Programming<\/a>&#8220;.<\/p>\n<p>What is the benefit of a regular type? Alexander Stepanov gives the answer in his book &#8220;<a href=\"http:\/\/elementsofprogramming.com\/\">Elements of Programming<\/a>&#8220;:&nbsp; <em><span dir=\"ltr\" role=\"presentation\">There is a set of procedures whose inclusion in the computational basis of a <\/span><span dir=\"ltr\" role=\"presentation\">type lets us place objects in data structures and use algorithms to copy ob<\/span><span dir=\"ltr\" role=\"presentation\">jects from one data structure to another. We call types having such a basis <\/span><span dir=\"ltr\" role=\"presentation\">regular<\/span><span dir=\"ltr\" role=\"presentation\">, since their use guarantees regularity of behavior and, therefore, in<\/span><span dir=\"ltr\" role=\"presentation\">teroperability.<\/span><\/em><span dir=\"ltr\" role=\"presentation\"> <\/span><\/p>\n<p>To make it short, regular types behave intuitively in data structures and algorithms such as built-in types.<\/p>\n<p>What does it mean for a regular type to be intuitive? According to the paper <a href=\"http:\/\/stepanovpapers.com\/DeSt98.pdf\">&#8220;Fundamentals of Generic Programming<\/a>&#8220;, a regular type should support the following operations:<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6504\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/RegularTypeStepanov.png\" alt=\"RegularTypeStepanov\" width=\"450\" height=\"189\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/RegularTypeStepanov.png 504w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/RegularTypeStepanov-300x126.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>&nbsp;I assume these operations are familiar to you. In C++20, we have the concept <code>std::regular<\/code> in C++20.<\/p>\n<h3>The Concept <code>std::regular<\/code><\/h3>\n<p>The concept <code>std::regular<\/code> is pretty similar to Stepanovs idea of regular types. It includes essentially move semantics:<\/p>\n<p>&nbsp;In C++20, we refine the idea of regular types into two concepts:<code> std::semiregular<\/code> and<code> std::regular<\/code>.<\/p>\n<h4><code>std:::semiregular<\/code><\/h4>\n<p>A semiregular type<code> X<\/code> has to support the rule of six (see my last post &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/rule-of-zero-or-six\">The Rule of Zero\/Six<\/a>&#8221; and has to be swappable.<\/p>\n<ul>\n<li>Default constructor:<code> X()<\/code><\/li>\n<li>Copy constructor: <code>X(const X&amp;)<\/code><\/li>\n<li>Copy assignment: <code>X&amp; operator = (const X&amp;)<\/code><\/li>\n<li>Move constructor: <code>X(X&amp;&amp;)<\/code><\/li>\n<li>Move assignment:<code> X&amp; operator = (X&amp;&amp;)<\/code><\/li>\n<li>Destructor: <code>~X()<\/code><\/li>\n<li>Swappable:<code> swap(X&amp;, X&amp;)<\/code><\/li>\n<\/ul>\n<p>Only one property is left, and a semiregular type X becomes regular.<\/p>\n<h4><code>std::regular<\/code><\/h4>\n<p>A regular type T is regular and equality comparable:<\/p>\n<ul>\n<li>Equality operator:<code> operator == (const X&amp;, const X&amp;)<\/code><\/li>\n<li>Inequality operator:<code> operator != (const X&amp;, const X&amp;)<\/code><\/li>\n<\/ul>\n<p>&nbsp;The concept<code> std::regular<\/code> is in C++20 based on the three concepts <code>std::movable<\/code>, <code>std::copyable<\/code>, and <code>std::semiregular<\/code>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\nconcept movable <span style=\"color: #555555;\">=<\/span> is_object_v<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #555555;\">&amp;&amp;<\/span> move_constructible<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #555555;\">&amp;&amp;<\/span>\r\nassignable_from<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&amp;<\/span>, T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #555555;\">&amp;&amp;<\/span> swappable<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\nconcept copyable <span style=\"color: #555555;\">=<\/span> copy_constructible<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #555555;\">&amp;&amp;<\/span> movable<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #555555;\">&amp;&amp;<\/span> assignable_from<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&amp;<\/span>, <span style=\"color: #006699; font-weight: bold;\">const<\/span> T<span style=\"color: #555555;\">&amp;&gt;<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\nconcept semiregular <span style=\"color: #555555;\">=<\/span> copyable<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #555555;\">&amp;&amp;<\/span> default_constructible<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\nconcept regular <span style=\"color: #555555;\">=<\/span> semiregular<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #555555;\">&amp;&amp;<\/span> equality_comparable<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The concept <span style=\"font-family: courier new, courier;\">std::movable<\/span> is based on the type-traits function<span style=\"font-family: courier new, courier;\"> <\/span><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/is_object\"><span style=\"font-family: courier new, courier;\">std::s_object<\/span><\/a>. Here is a possible implementation from<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/is_object\"> cppreference.com<\/a>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T<\/span><span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> is_object <span style=\"color: #555555;\">:<\/span> std<span style=\"color: #555555;\">::<\/span>integral_constant<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">bool<\/span>,\r\n                     std<span style=\"color: #555555;\">::<\/span>is_scalar<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>value <span style=\"color: #555555;\">||<\/span>\r\n                     std<span style=\"color: #555555;\">::<\/span>is_array<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>value  <span style=\"color: #555555;\">||<\/span>\r\n                     std<span style=\"color: #555555;\">::<\/span>is_union<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>value  <span style=\"color: #555555;\">||<\/span>\r\n                     std<span style=\"color: #555555;\">::<\/span>is_class<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>value<span style=\"color: #555555;\">&gt;<\/span> {};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Furthermore, the concept <code>std::movable<\/code> is based on the concepts<code> std::move_constructible<\/code> and<code> std::assignable_from<\/code>. All other components of the concepts <code>std::copyable<\/code> and <code>std::semiregular<\/code> are also C++20 concepts.<\/p>\n<p>When you want to know more about concepts, read my post: <a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/concepts\">concepts<\/a>.<\/p>\n<p>You may wonder: Which type is not regular? The most prominent one is probably a reference.<\/p>\n<h3>References<\/h3>\n<p>A reference is not an object and is neither regular nor semiregular.<\/p>\n<p>Let&#8217;s try it out:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ referenceIsObject.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;functional&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;type_traits&gt;<\/span>\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::is_object&lt;int&amp;&gt;::value: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>is_object<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&amp;&gt;::<\/span>value <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;  <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::is_object&lt;std::reference_wrapper&lt;int&gt;&gt;::value: \"<\/span>                  <em><span style=\"color: #0099ff;\"> \/\/ (2)<\/span><\/em>\r\n              <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>is_object<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>reference_wrapper<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&gt;::<\/span>value <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The output of the program shows is. A reference (line 1) is not an object:<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6505\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/referenceIsObject.png\" alt=\"referenceIsObject\" width=\"550\" height=\"209\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/referenceIsObject.png 616w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/referenceIsObject-300x114.png 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>You may wonder about line 2 in the program <code>referenceIsObject.cpp.<\/code> A reference<code> int&amp;<\/code> is not an object, but a reference wrapper <code>std::reference_wrapper&lt;int&gt;<\/code> is one. Here is the description of a reference wrapper on <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/functional\/reference_wrapper\">cppreference.com:<\/a><\/p>\n<p><em><code>std::reference_wrapper<\/code> is a class template that wraps a reference in a copyable, assignable object. It is frequently used as a mechanism to store references inside standard containers (like <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/vector\" title=\"cpp\/container\/vector\">std::vector<\/a><\/span>) which cannot normally hold references.&nbsp;<\/em><\/p>\n<p>This means that a vector of references <code>std::vector&lt;int&amp;&gt;<\/code> is not valid, but a vector of reference wrappers <code>std::vector&lt;std::reference_wrapper&lt;int&gt;<\/code>&gt; is. Consequently, since C++11, you can have a container with reference semantics.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ referenceSemantics.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;functional&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;list&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;type_traits&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>list<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myList{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>};\r\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>reference_wrapper<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&gt;<\/span> myRefVector(myList.begin(), myList.end());\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> l<span style=\"color: #555555;\">:<\/span> myList) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> l <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span><span style=\"color: #555555;\">&amp;<\/span> v<span style=\"color: #555555;\">:<\/span> myRefVector) v <span style=\"color: #555555;\">*=<\/span> v;            <em><span style=\"color: #0099ff;\"> \/\/ (1)<\/span><\/em>\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> l<span style=\"color: #555555;\">:<\/span> myList) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> l <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;    <em><span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;Modifying the elements of a <code>std::vector&lt;std::reference_wrapper&lt;int&gt;&gt;<\/code> (line 1) also modifies the referenced objects (line 2).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6506\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/referenceSemantics.png\" alt=\"referenceSemantics\" width=\"400\" height=\"235\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/referenceSemantics.png 428w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/referenceSemantics-300x176.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Let&#8217;s go one step back. What happens in C++20 when you have an algorithm that requires a regular type but uses a reference?<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ needRegularType.cpp <\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;concepts&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>regular T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyClass<\/span>{};\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    MyClass<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myClass1;\r\n    MyClass<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&amp;&gt;<\/span> myClass2;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I compiled the program with the latest GCC 12.2, the latest clang 15.0.0, and the latest MSVC v19 compiler. The Microsoft compiler provided the best error message:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5838\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/01\/RegularError.png\" alt=\"RegularError\" width=\"650\" height=\"426\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>&nbsp;A value object is a small object whose equality is based on the statem but not on its identity. They will be the topic of my next post.<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The concept of a regular type goes back to the creator of the Standard Template Library (STL) Alexander Stepanov. A regular type is a user-defined type and behaves like a built-in type.<\/p>\n","protected":false},"author":21,"featured_media":6503,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[494],"class_list":["post-6507","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns","tag-move"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6507","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=6507"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6507\/revisions"}],"predecessor-version":[{"id":8108,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6507\/revisions\/8108"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6503"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}