{"id":5768,"date":"2019-08-30T06:22:19","date_gmt":"2019-08-30T06:22:19","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/more-myths-of-my-blog-readers\/"},"modified":"2019-08-30T06:22:19","modified_gmt":"2019-08-30T06:22:19","slug":"more-myths-of-my-blog-readers","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/more-myths-of-my-blog-readers\/","title":{"rendered":"More Myths of My Blog Readers"},"content":{"rendered":"<p>Today, I conclude my story to your myths about C++. These myths are around function parameters, the initialisation of class members, and pointer versus references.<\/p>\n<p><!--more--><\/p>\n<h2>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5763\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/dragon-4417431_1280.png\" alt=\"dragon 4417431 1280\" width=\"600\" height=\"400\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/dragon-4417431_1280.png 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/dragon-4417431_1280-300x200.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/dragon-4417431_1280-1024x682.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/dragon-4417431_1280-768x512.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/h2>\n<h2>Always take the parameter by const reference (Gunter K\u00f6nigsmann)<\/h2>\n<p>You have two options when a function takes its parameter and doesn&#8217;t want to modify it.<\/p>\n<ul>\n<li>Take the parameter by value (copy it)<\/li>\n<li>Take the parameter by const reference<\/li>\n<\/ul>\n<p>This was the correctness perspective, but what can be said about the performance? The C++ core guidelines are specific about performance. Let&#8217;s look at the following example.<\/p>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f1<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> string<span style=\"color: #555555;\">&amp;<\/span> s);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK: pass by reference to const; always cheap<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f2<\/span>(string s);         <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad: potentially expensive<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f3<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> x);            <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK: Unbeatable<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f4<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&amp;<\/span> x);     <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad: overhead on access in f4()<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Presumably, based on experience, the guidelines state a rule of thumb:<\/p>\n<ul>\n<li><strong>You should take a parameter p by const reference&nbsp;if <span style=\"font-family: courier new, courier;\">sizeof(p) &gt; 4 * sizeof(int)<\/span><\/strong><\/li>\n<li><strong>You should copy a parameter&nbsp;<span style=\"font-family: courier new, courier;\">p<\/span> if <span style=\"font-family: courier new, courier;\">sizeof(p) &lt; 3 * sizeof(int)<\/span><\/strong><\/li>\n<\/ul>\n<p>Okay, now you should know how big your data types are. The program <span style=\"font-family: courier new, courier;\">sizeofArithmeticTypes.cpp<\/span> gives the answers for arithmetic types.<\/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;\">\/\/ sizeofArithmeticTypes.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&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> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(void*): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">*<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;  \r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(5):  \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #ff6600;\">5<\/span>)   <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(5l): \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #ff6600;\">5l<\/span>)  <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(5ll): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #ff6600;\">5ll<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(5.5f): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #ff6600;\">5.5f<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(5.5): \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #ff6600;\">5.5<\/span>)  <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sizeof(5.5l): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #ff6600;\">5.5<\/span>l) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;       \r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n            \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: courier new, courier;\">sizeof(void*)<\/span> returns if it is a 32-bit or a 64-bit system. Thanks to an online compiler <a href=\"https:\/\/rextester.com\/\">rextester<\/a>, I can execute the program with GCC, Clang, and cl.exe (Windows). Here are the numbers for all 64-bit systems.<\/p>\n<\/p>\n<h3>GCC<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5764\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/sizeofGCC.png\" alt=\"sizeofGCC\" style=\"display: block; margin-left: auto; margin-right: auto;\" width=\"167\" height=\"204\" \/><\/p>\n<h3>Clang<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5765\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/sizeofClang.png\" alt=\"sizeofClang\" style=\"display: block; margin-left: auto; margin-right: auto;\" width=\"172\" height=\"213\" \/><\/p>\n<h3>cl.exe (Windows)<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5766\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/sizeofVC.png\" alt=\"sizeofVC\" style=\"display: block; margin-left: auto; margin-right: auto;\" width=\"158\" height=\"209\" \/><\/p>\n<p>cl.exe behaves differently from GCC and Clang. A<span style=\"font-family: courier new, courier;\"> long int<\/span> has only 4 bytes, and a <span style=\"font-family: courier new, courier;\">long double<\/span> has 8 bytes. On GCC and Clang, <span style=\"font-family: courier new, courier;\">long int<\/span> and long <span style=\"font-family: courier new, courier;\">double have<\/span> double size.<\/p>\n<p>Deciding when to take the parameter by value or by const reference is just math. If you want to know the exact performance numbers for your architecture, there is only one answer: <strong>measure<\/strong>.<\/p>\n<h2>Initialization and Assignment in the Constructor are equivalent (Gunter K\u00f6nigsmann)<\/h2>\n<p>First, let me show you initialization and assignment in the constructor.<\/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;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Good<\/span>{  \r\n    int i;\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    Good(int i_)<span style=\"color: #555555;\">:<\/span> i{i_<span style=\"color: #cc3300;\"><\/span>}{} \r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Bad<\/span>{  \r\n    int i;\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    Bad(int i_)<span style=\"color: #555555;\">:<\/span> { i <span style=\"color: #555555;\">= i_<\/span><span style=\"color: #cc3300;\"><\/span>; } \r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The class <span style=\"font-family: 'courier new', courier;\">Good<\/span> uses initialization but the class <span style=\"font-family: 'courier new', courier;\">Bad<\/span> assignment. The consequences are:<\/p>\n<ul>\n<li>The variable<span style=\"font-family: courier new, courier;\"> i<\/span> is directly initialized in the class <span style=\"font-family: courier new, courier;\">Good<\/span><\/li>\n<li>The variable<span style=\"font-family: courier new, courier;\"> i<\/span> is default constructed and then assigned to the class <span style=\"font-family: courier new, courier;\">Bad<\/span><\/li>\n<\/ul>\n<p>&nbsp;The constructor initialization is, on the one hand, slower but does not work on the other hand for const members, references, or members which can not be default-constructed possible.<\/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;\">\/\/ constructorAssignment.cpp<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> NoDefault{\r\n    NoDefault(<span style=\"color: #007788; font-weight: bold;\">int<\/span>){};\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Bad<\/span>{\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> constInt;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&amp;<\/span> refToInt;\r\n    NoDefault noDefault;\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    Bad(<span style=\"color: #007788; font-weight: bold;\">int<\/span> i, <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&amp;<\/span> iRef){\r\n        constInt <span style=\"color: #555555;\">=<\/span> i;\r\n        refToInt <span style=\"color: #555555;\">=<\/span> iRef;\r\n    }\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ Bad(int i, int&amp; iRef): constInt(i), refToInt(iRef), noDefault{i} {}<\/span>\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">10<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&amp;<\/span> j <span style=\"color: #555555;\">=<\/span> i;\r\n  \r\n    Bad bad(i, j);\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When I try to compile the program, I get three different errors.<\/p>\n<ol>\n<li><span style=\"font-family: courier new, courier;\">constInt<\/span> is not initialized and can not be assigned in the constructor.<\/li>\n<li><span style=\"font-family: courier new, courier;\">refToIn<\/span>t is not initialized.<\/li>\n<li>The class<span style=\"font-family: courier new, courier;\"> NoDefault<\/span> has no default constructor because I implemented one constructor for<span style=\"font-family: courier new, courier;\"> int<\/span>. When implementing a constructor, the compiler will not automatically generate a default constructor.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5767\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/constructorAssignment.png\" alt=\"constructorAssignment\" width=\"700\" height=\"335\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/constructorAssignment.png 1153w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/constructorAssignment-300x144.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/constructorAssignment-1024x490.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/08\/constructorAssignment-768x368.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>In the second successful compilation, I used the second commented-out constructor, which uses initialization instead of assignment.<\/p>\n<p>The example used references instead of raw pointers for a good reason.<\/p>\n<h2>You need Raw Pointers in your Code (<span class=\"full_user_string\"><span class=\"pseudonym\">Thargon110<\/span><\/span>)<\/h2>\n<p>Motivated by a comment from Thargon110, I want to be dogmatic: NNN. What? I mean <strong>N<\/strong>o <strong>Na<\/strong>ked <strong>N<\/strong>ew. From an application perspective, there is no reason to use raw pointers. If you need a pointer like semantic, put your pointer into an smart pointer (You see: NNN), and you are done.<\/p>\n<p>In essence, C++11 has a <span style=\"font-family: courier new, courier;\">std::unique_ptr<\/span> for exclusive ownership and a <span style=\"font-family: courier new, courier;\">std::shared_ptr<\/span> for shared ownership. Consequently, when you copy a <span style=\"font-family: courier new, courier;\">std::shared_ptr<\/span>, the reference counter is incremented, and when you delete the <span style=\"font-family: courier new, courier;\">std::shared_ptr,<\/span> the reference counter is decremented. Ownership means that the smart pointer keeps track of the underlying memory and releases the memory if it is not necessary anymore. The memory is not necessary any more in the case of the std::shared_ptr when the reference counter becomes 0.<\/p>\n<p>So memory leaks are gone with modern C++. Now I hear your complaints. I&#8217;m happy to destroy them.<\/p>\n<ul>\n<li>Cycles of <span style=\"font-family: courier new, courier;\">std::shared_ptr<\/span> can create a memory leak because the reference counter will not become 0. Right, put a <span style=\"font-family: courier new, courier;\">std::weak_ptr<\/span> in-between to break the cyclic reference:<a href=\"https:\/\/www.modernescpp.com\/index.php\/std-weak-ptr\"> std::weak_ptr. <\/a><\/li>\n<li>A <span style=\"font-family: courier new, courier;\">std::shared_ptr<\/span> has a management overhead and is, therefore, more expensive than a raw pointer. Right, use a <a href=\"https:\/\/www.modernescpp.com\/index.php\/std-unique-ptr\"><span style=\"font-family: courier new, courier;\">std::unique_ptr.<\/span><\/a><\/li>\n<li>A <span style=\"font-family: courier new, courier;\">std::unique_ptr<\/span> is not comfortable enough because it can&#8217;t be copied. Right, but a <span style=\"font-family: courier new, courier;\">std::unique_ptr<\/span> can be moved.<\/li>\n<\/ul>\n<p>&nbsp;The last complaint is quite dominant. A small example should make my point:<\/p>\n<p>&nbsp;<\/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;\">\/\/ moveUniquePtr.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;algorithm&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;memory&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;utility&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">takeUniquePtr<\/span>(std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> uniqPtr){          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"*uniqPtr: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #555555;\">*<\/span>uniqPtr <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n}\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> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> uniqPtr1 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">2014<\/span>);\r\n    \r\n    takeUniquePtr(std<span style=\"color: #555555;\">::<\/span>move(uniqPtr1));                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> uniqPtr2 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">2017<\/span>);\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> uniqPtr3 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">2020<\/span>);\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> uniqPtr4 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">2023<\/span>);\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&gt;<\/span> vecUniqPtr;\r\n    vecUniqPtr.push_back(std<span style=\"color: #555555;\">::<\/span>move(uniqPtr2));             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    vecUniqPtr.push_back(std<span style=\"color: #555555;\">::<\/span>move(uniqPtr3));             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    vecUniqPtr.push_back(std<span style=\"color: #555555;\">::<\/span>move(uniqPtr4));             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/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>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>for_each(vecUniqPtr.begin(), vecUniqPtr.end(),    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n                  [](std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&amp;<\/span> uniqPtr){ std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span>  <span style=\"color: #555555;\">*<\/span>uniqPtr <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; } );\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The function <span style=\"font-family: courier new, courier;\">takeUniquePtr<\/span> in line (1) takes a <span style=\"font-family: courier new, courier;\">std::unique_ptr<\/span> by value. The critical observation is that you have to move the <span style=\"font-family: courier new, courier;\">std::unique_ptr<\/span> inside. The same argument holds for the <span style=\"font-family: courier new, courier;\">std::vector&lt;std::unique_ptr&lt;int&gt;&gt; (line 2)<\/span>. <span style=\"font-family: courier new, courier;\">std::vector <\/span>as all containers of the standard template library want to own its elements, but to copy a <span style=\"font-family: courier new, courier;\">std::unique_ptr<\/span> is not possible. <span style=\"font-family: courier new, courier;\">std::move<\/span> solves this issue. You can apply an algorithm such as <span style=\"font-family: courier new, courier;\">std::for_each<\/span> on the <span style=\"font-family: courier new, courier;\">std::vector&lt;std::unique_ptr&lt;int&gt;&gt;<\/span>&nbsp;(line 3) if no copy semantic is used.<\/p>\n<h3>Use References instead of Raw Pointers<\/h3>\n<p>In the end, I want to refer to the critical concern of <span class=\"full_user_string\"><span class=\"pseudonym\">Thargon110. Admittedly, this rule is way more important in classical C++ without smart pointers because smart pointers are in contrast to raw pointers owners.<\/span><\/span><\/p>\n<p><span class=\"full_user_string\"><span class=\"pseudonym\">Use a reference instead of a pointer because a reference always has a value. Tedious checks such as the following one are gone with references.&nbsp;<br \/><\/span><\/span><\/p>\n<p>&nbsp;<\/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;\">if<\/span>(<span style=\"color: #555555;\">!<\/span>ptr){\r\n   std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Something went terrible wrong\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n   <span style=\"color: #006699; font-weight: bold;\">return<\/span>;\r\n}\r\nstd<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"All fine\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Additionally, you can forget the check.&nbsp;References behave just as constant pointers.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The C++ core guidelines define profiles. Profiles are a subset of rules. They exist for type safety, bounds safety, and lifetime safety. They will be my <a href=\"https:\/\/www.modernescpp.com\/index.php\/profiles-in-the-c-core-guidelines\">next topic<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I conclude my story to your myths about C++. These myths are around function parameters, the initialisation of class members, and pointer versus references.<\/p>\n","protected":false},"author":21,"featured_media":5763,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[],"class_list":["post-5768","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5768","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=5768"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5768\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5763"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}