{"id":5391,"date":"2018-02-16T20:07:45","date_gmt":"2018-02-16T20:07:45","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-do-s-and-don-ts\/"},"modified":"2023-06-26T11:56:14","modified_gmt":"2023-06-26T11:56:14","slug":"c-core-guidelines-do-s-and-don-ts","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-do-s-and-don-ts\/","title":{"rendered":"C++ Core Guidelines:  Rules about Don&#8217;ts"},"content":{"rendered":"<p>This post is about don&#8217;ts. Here are this post&#8217;s two most important rules: Don&#8217;t use <span style=\"font-family: 'courier new', courier;\">std::move<\/span> thoughtless and don&#8217;t slice. Let&#8217;s start.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5389\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/Black_Forest_gateau20.png\" alt=\"Black Forest gateau20\" style=\"display: block; margin-left: auto; margin-right: auto;\" width=\"614\" height=\"461\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/Black_Forest_gateau20.png 614w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/Black_Forest_gateau20-300x225.png 300w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Here are the don&#8217;ts for today.<\/p>\n<ul>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-move\">ES.56: Write <code class=\"highlighter-rouge no-highlight\">std::move()<\/code> only when you need to explicitly move an object to another scope<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-new\">ES.60: Avoid <code class=\"highlighter-rouge no-highlight\">new<\/code> and <code class=\"highlighter-rouge no-highlight\">delete<\/code> outside resource management functions<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-del\">ES.61: Delete arrays using <code class=\"highlighter-rouge no-highlight\">delete[]<\/code> and non-arrays using <code class=\"highlighter-rouge no-highlight\">delete<\/code><\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-slice\">ES.63: Don\u2019t slice<\/a><\/li>\n<\/ul>\n<p>The first rule is a disguised don&#8217;t.&nbsp;<\/p>\n<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-move\">ES.56: Write <code class=\"highlighter-rouge no-highlight\">std::move()<\/code> only when you need to explicitly move an object to another scope<\/a><\/h3>\n<p>Most of the time, there is no need to call std::move explicitly<span style=\"font-family: 'courier new', courier;\">.<\/span>The compiler automatically applies to move semantics if the source of the operation is an rvalue. An rvalue is an object with no identity. An rvalue typically has no name, and you can not get its address. The remaining objects are lvalues.&nbsp;<\/p>\n<p>Applying <span style=\"font-family: 'courier new', courier;\">std::move<\/span> to a lvalue gives, most of the time, an empty object. The lvalue is afterward in a so-called moved-from state. This means that it is in a valid but no nearer specified state. Sound strange? Right! You just have keep this rule in mind: After you move from a lvalue such as <span style=\"font-family: 'courier new', courier;\">std::move(source)<\/span> you can not make any assumption about <span style=\"font-family: 'courier new', courier;\">source. <\/span>You have to set it&nbsp;to a new value.<\/p>\n<p>Wait for a second. The rule says you should only use <span style=\"font-family: 'courier new', courier;\">std::move<\/span> to move an object to another scope. The classical use cases are objects which can not be copied but moved. For example, you want to move a <span style=\"font-family: 'courier new', courier;\">std::promise<\/span> into another thread.&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;\">\/\/ moveExplicit.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;future&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;utility&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">product<\/span>(std<span style=\"color: #555555;\">::<\/span>promise<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&amp;&amp;<\/span> intPromise, <span style=\"color: #007788; font-weight: bold;\">int<\/span> a, <span style=\"color: #007788; font-weight: bold;\">int<\/span> b){     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n  intPromise.set_value(a <span style=\"color: #555555;\">*<\/span> b);\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> a<span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">20<\/span>;\r\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> b<span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">10<\/span>;\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ define the promises<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>promise<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> prodPromise;\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ get the futures<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>future<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> prodResult<span style=\"color: #555555;\">=<\/span> prodPromise.get_future();\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ calculate the result in a separat thread<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> prodThread(product,std<span style=\"color: #555555;\">::<\/span>move(prodPromise), a, b);   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n \r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ get the result<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"20 * 10 = \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> prodResult.get() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;     <span style=\"color: #0099ff; font-style: italic;\">\/\/ 200<\/span>\r\n  \r\n  prodThread.join();\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The function <span style=\"font-family: 'courier new', courier;\">product<\/span> (1) gets the <span style=\"font-family: 'courier new', courier;\">std::promise<\/span> by rvalue reference. A promise cannot be copied but moved; therefore, <span style=\"font-family: 'courier new', courier;\">std::move<\/span> is necessary (2) to move the promise into the newly created thread.&nbsp;<\/p>\n<p>Here is the big <strong>don&#8217;t! Don&#8217;t use <span style=\"font-family: 'courier new', courier;\">std::move<\/span> in a return statement.&nbsp;<\/strong><\/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%;\">vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> make_vector() {\r\n    vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> result;\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ... load result with data<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>move(result);       <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad; just write \"return result;\"<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span>Trust your optimizer! If you return the object just by copying, the optimizer will do its job. This is a best practice until C++14; this is an obligatory rule since C++17 and is called guaranteed copy elision. Although this technique is called automatic copy elision, move operations are also optimized away with C++11.<\/span><\/p>\n<p><strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Return_value_optimization\">RVO<\/a><\/strong> stands for <strong>R<\/strong>eturn <strong>V<\/strong>alue <strong>Optimization<\/strong> and means that the compiler is allowed to remove unnecessary copy operations. What was until C++14 a possible optimization step becomes a in C++17 guarantee.<\/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%;\">MyType <span style=\"color: #cc00ff;\">func<\/span>(){\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> MyType{};         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1) no copy with C++17<\/span>\r\n}\r\nMyType myType <span style=\"color: #555555;\">=<\/span> func();    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2) no copy with C++17<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Two unnecessary copy operations can happen in these few lines\u2014the first one in (1) and the second one in (2). With C++17, both copy operations are not allowed.<\/p>\n<p>If the return value has a name, it&#8217;s called&nbsp;<strong>NRVO.<\/strong>&nbsp;This acronym stands for<strong> N<\/strong>amed <strong>R<\/strong>eturn <strong>V<\/strong>alue <strong>O<\/strong>ptimization.<\/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%;\">MyType <span style=\"color: #cc00ff;\">func<\/span>(){\r\n  MyType myVal;\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> myVal;            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1) one copy allowed <\/span>\r\n}\r\nMyType myType <span style=\"color: #555555;\">=<\/span> func();    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2) no copy with C++17<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The subtle difference is that the compiler can still copy the value <span style=\"font-family: 'courier new', courier;\">myValue&nbsp;<\/span>according to C++17 (1). But no copy will take place in (2).&nbsp;<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-new\">ES.60: Avoid <code class=\"highlighter-rouge no-highlight\">new<\/code> and <code class=\"highlighter-rouge no-highlight\">delete<\/code> outside resource management functions<\/a><\/h3>\n<p>&nbsp;Okay, I can make it short. Don&#8217;t use <span style=\"font-family: 'courier new', courier;\">new<\/span> and <span style=\"font-family: 'courier new', courier;\">delete<\/span>&nbsp;the application code. This rule has a friendly reminder: &#8220;No naked new!&#8221;.&nbsp;<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-del\">ES.61: Delete arrays using <code class=\"highlighter-rouge no-highlight\">delete[]<\/code> and non-arrays using <code class=\"highlighter-rouge no-highlight\">delete<\/code><\/a><\/h3>\n<p>Here is the rationale for the last rule. Resource management in application code is error-prone.<\/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: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> n)\r\n{\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> p <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> X[n];   <span style=\"color: #0099ff; font-style: italic;\">\/\/ n default constructed Xs<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">delete<\/span> p;   <span style=\"color: #0099ff; font-style: italic;\">\/\/ error: just delete the object p, rather than delete the array p[]<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The guidelines state in the comment: &#8220;just delete the object p&#8221;. Let me put it more drastically. This is undefined behavior!<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-slice\">ES.63: Don\u2019t slice<\/a><\/h3>\n<p>First of all. What is slicing? Slicing means: you want to copy an object during assignment or initialization, and you get only a part of the object.&nbsp;<\/p>\n<p>Let&#8217;s start simple.<\/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;\">\/\/ slice.cpp<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Base { \r\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> base{<span style=\"color: #ff6600;\">1998<\/span>};\r\n}\r\n \r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Derived <span style=\"color: #555555;\">:<\/span> Base { \r\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> derived{<span style=\"color: #ff6600;\">2011<\/span>};\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> needB(Base b){\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n}\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\r\n\r\n  Derived d;\r\n  Base b <span style=\"color: #555555;\">=<\/span> d;              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n  Base <span style=\"color: #cc00ff;\">b2<\/span>(d);              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n  needB(d);                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The lines (1), (2), and (3) have all the same effect: the <span style=\"font-family: 'courier new', courier;\">Derived<\/span> part of <span style=\"font-family: 'courier new', courier;\">d<\/span> is removed. I assume that was not your intention.<\/p>\n<p>In the announcement to this post, I said that slicing is one of the darkest parts of C++. Now it becomes dark.<\/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;\">\/\/ sliceVirtuality.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Base { \r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"Base\"<\/span>;       \r\n    }\r\n};\r\n \r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Derived <span style=\"color: #555555;\">:<\/span> Base { \r\n    std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override {       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"Derived\"<\/span>;\r\n    }\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    Base b;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"b.getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> b.getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    \r\n    Derived d;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"d.getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> d.getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    \r\n    Base b1 = d;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"b1.getName():  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> b1.getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n   \r\n    Base<span style=\"color: #555555;\">&amp;<\/span> b2 <span style=\"color: #555555;\">=<\/span> d;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"b2.getName():  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> b2.getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\r\n\r\n    Base<span style=\"color: #555555;\">*<\/span> b3 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> Derived;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"b3-&gt;getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> b3<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (7)<\/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}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I created a small hierarchy consisting of the <span style=\"font-family: 'courier new', courier;\">Base<\/span> and the <span style=\"font-family: 'courier new', courier;\">Derived<\/span> class. Each object of this class hierarchy should return its name. I made the method <span style=\"font-family: 'courier new', courier;\">getName<\/span> virtual (1) and overrode it in (2); therefore, I will have polymorphism. I can use a derived object via a reference (6) or a pointer to a base object (7). Under the hood, the object is of type <span style=\"font-family: 'courier new', courier;\">Derived<\/span>.&nbsp;<\/p>\n<p>This will not hold if I just copy <span style=\"font-family: 'courier new', courier;\">Derived d<\/span> to <span style=\"font-family: 'courier new', courier;\">Base b1 (5)<\/span>. In this case, slicing kicks in, and I have a <span style=\"font-family: 'courier new', courier;\">Base<\/span> object under the hood. In the case of copying, the declared or static type is used. The actual or dynamic type is used if you use an indirection such as a reference or a pointer.&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5390\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/sliceVirtuality.png\" alt=\"sliceVirtuality\" style=\"display: block; margin-left: auto; margin-right: auto;\" width=\"446\" height=\"272\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/sliceVirtuality.png 446w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/sliceVirtuality-300x183.png 300w\" sizes=\"auto, (max-width: 446px) 100vw, 446px\" \/><\/p>\n<p>To keep the rule in mind is quite simple: If your instances of a class should be polymorphic, it should declare or inherit at least one virtual method, and you should use its objects via an indirection such as a pointer or a reference.&nbsp;<\/p>\n<p>Of course, there is a cure for slicing: provide a virtual clone function. Read the details here: <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-copy-and-move-rules\">C++ Core Guidelines: Rules for Copy and Move.<\/a><\/p>\n<h2>What&#8217;s next<\/h2>\n<p>This post was about don&#8217;ts. The <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-for-statements\">next post<\/a> will start with a do. Use curly braces for the initialization of data.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is about don&#8217;ts. Here are this post&#8217;s two most important rules: Don&#8217;t use std::move thoughtless and don&#8217;t slice. Let&#8217;s start.<\/p>\n","protected":false},"author":21,"featured_media":5389,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[494,493],"class_list":["post-5391","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-move","tag-new-delete"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5391","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=5391"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5391\/revisions"}],"predecessor-version":[{"id":6839,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5391\/revisions\/6839"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5389"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}