{"id":2280,"date":"2021-01-17T17:29:41","date_gmt":"2021-01-17T11:59:41","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2280"},"modified":"2021-01-17T17:29:41","modified_gmt":"2021-01-17T11:59:41","slug":"c20-nodiscard-attribute","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/","title":{"rendered":"C++20 [[nodiscard]] Attribute"},"content":{"rendered":"<p><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/attributes\/nodiscard\" target=\"_blank\" rel=\"noopener\"><em><strong>[[nodiscard]]<\/strong><\/em><\/a>\u00a0attribute <em><strong>\u201c<\/strong>encourages<strong>\u201d<\/strong><\/em> the compiler to issue a warning when the return value from a function is ignored. It was introduced in <em><strong>C++ 17<\/strong><\/em> and enhanced in <em><strong>C++ 20<\/strong><\/em> to include a string literal that can be used as an explanation of the warning.<\/p>\n<p>Let us look at different cases one by one.<\/p>\n<h4>Case-1: An enumeration is marked [[nodiscard]]<\/h4>\n<p>When a function is defined to return this enumeration, the compiler checks to see if the returned value is discarded. If it is, then the compiler is likely to generate a warning.<\/p>\n<p>Look at the following code:<\/p>\n<figure id=\"attachment_2281\" aria-describedby=\"caption-attachment-2281\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2281\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/example1-src\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src.jpg\" data-orig-size=\"813,708\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1610891047&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example1 src\" data-image-description=\"&lt;p&gt;Case-1: Enumeration&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Case-1: Enumeration&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src.jpg\" class=\"wp-image-2281\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src.jpg?resize=550%2C479&#038;ssl=1\" alt=\"Case-1: Enumeration\" width=\"550\" height=\"479\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src.jpg?resize=300%2C261&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src.jpg?resize=768%2C669&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src.jpg?w=813&amp;ssl=1 813w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2281\" class=\"wp-caption-text\"><strong>Case-1: Enumeration<\/strong><\/figcaption><\/figure>\n<p>Here, the enum class <em><strong>\u201cDeviceStatus\u201d<\/strong><\/em> is marked <em><strong>[[nodiscard]]<\/strong><\/em>, and the function <em><strong>\u201cgetDeviceStatus()\u201d<\/strong><\/em> returns one of the elements of this enum. Next, look at the three calls to this function inside <em><strong>\u201cmain\u201d<\/strong><\/em>. In Line 19, the return value from the function is ignored and this is caught by the compiler. In Line 22, the return value is checked and hence it is OK. In Line 27, we explicitly typecast the result to <em><strong>\u201cvoid\u201d,<\/strong><\/em> indicating that we are ignoring the return value deliberately and hence this is also acceptable (of course, why we chose to ignore is a different issue).<\/p>\n<p>Here is the compiler output:<\/p>\n<figure id=\"attachment_2282\" aria-describedby=\"caption-attachment-2282\" style=\"width: 551px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2282\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/example1-21\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1.jpg\" data-orig-size=\"1071,278\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1610890881&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Compiler Output\" data-image-description=\"&lt;p&gt;Compiler Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Compiler Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-1024x266.jpg\" class=\"wp-image-2282\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1.jpg?resize=551%2C143&#038;ssl=1\" alt=\"Compiler Output\" width=\"551\" height=\"143\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1.jpg?resize=300%2C78&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1.jpg?resize=1024%2C266&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1.jpg?resize=768%2C199&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1.jpg?w=1071&amp;ssl=1 1071w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/a><figcaption id=\"caption-attachment-2282\" class=\"wp-caption-text\"><strong>Compiler Output<\/strong><\/figcaption><\/figure>\n<h4>Case-2: A struct\/class is marked [[nodiscard]]<\/h4>\n<p>In this case, instead of an enumeration, we have a <em><strong>class<\/strong><\/em> or <em><strong>struct<\/strong><\/em> that is marked <em><strong>[[nodiscard]]<\/strong><\/em>.<\/p>\n<figure id=\"attachment_2284\" aria-describedby=\"caption-attachment-2284\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2-src.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2284\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/example2-src\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2-src.jpg\" data-orig-size=\"698,780\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1610891166&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Case-2: Struct\/Class\" data-image-description=\"&lt;p&gt;Case-2: Struct\/Class&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Case-2: Struct\/Class&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2-src.jpg\" class=\"wp-image-2284\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2-src.jpg?resize=550%2C615&#038;ssl=1\" alt=\"Case-2: Struct\/Class\" width=\"550\" height=\"615\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2-src.jpg?resize=268%2C300&amp;ssl=1 268w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2-src.jpg?w=698&amp;ssl=1 698w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2284\" class=\"wp-caption-text\"><strong>Case-2: Struct\/Class<\/strong><\/figcaption><\/figure>\n<p>In this case, the function <em><strong>\u201cfoo()\u201d<\/strong><\/em> returns <em><strong>\u201cMyType\u201d<\/strong><\/em> instance by <em><strong>\u201cvalue\u201d<\/strong><\/em>, whereas the function <em><strong>\u201cbar()\u201d<\/strong><\/em> returns by <em><strong>\u201creference\u201d<\/strong><\/em>. In Line 49, the returned value is ignored and hence this triggers a warning. Line 52 is fine. Interestingly, in Line 55, even though we are not using the return value, this does not generate any warning since the return is by <em><strong>\u201creference\u201d<\/strong><\/em>. This is as per the standard.<\/p>\n<p>Here is the compiler output:<\/p>\n<figure id=\"attachment_2285\" aria-describedby=\"caption-attachment-2285\" style=\"width: 552px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2285\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/example2-17\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2.jpg\" data-orig-size=\"1141,275\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1610891130&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Compiler Output\" data-image-description=\"&lt;p&gt;Compiler Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Compiler Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2-1024x247.jpg\" class=\"wp-image-2285\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2.jpg?resize=552%2C133&#038;ssl=1\" alt=\"Compiler Output\" width=\"552\" height=\"133\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2.jpg?resize=300%2C72&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2.jpg?resize=1024%2C247&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2.jpg?resize=768%2C185&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example2.jpg?w=1141&amp;ssl=1 1141w\" sizes=\"(max-width: 552px) 100vw, 552px\" \/><\/a><figcaption id=\"caption-attachment-2285\" class=\"wp-caption-text\"><strong>Compiler Output<\/strong><\/figcaption><\/figure>\n<h4>Case-3: Constructor is marked [[nodiscard]]<\/h4>\n<p><em><strong>C++20<\/strong><\/em> allows constructors to be tagged as <em><strong>[[nodiscard]]<\/strong><\/em> (without the struct\/class being so).<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<figure id=\"attachment_2286\" aria-describedby=\"caption-attachment-2286\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3-src.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2286\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/example3-src\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3-src.jpg\" data-orig-size=\"881,687\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1610891532&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Case-3: Constructor\" data-image-description=\"&lt;p&gt;Case-3: Constructor&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Case-3: Constructor&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3-src.jpg\" class=\"wp-image-2286\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3-src.jpg?resize=550%2C429&#038;ssl=1\" alt=\"Case-3: Constructor\" width=\"550\" height=\"429\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3-src.jpg?resize=300%2C234&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3-src.jpg?resize=768%2C599&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3-src.jpg?w=881&amp;ssl=1 881w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2286\" class=\"wp-caption-text\"><strong>Case-3: Constructor<\/strong><\/figcaption><\/figure>\n<p>Notice that there is no issue with Line 69, even though we are ignoring the function return value. This is because the struct <em><strong>\u201cSpecialType\u201d<\/strong> <\/em>is not marked <em><strong>[[nodiscard]]<\/strong><\/em>. However, there is a problem in Line 73. Since the constructor is marked <em><strong>[[nodiscard]]<\/strong><\/em>, the compiler sees that the constructed object is bound to a temporary and hence is not <em><strong>\u201cused\u201d<\/strong><\/em>. On the other hand, lines 76, 78 and 80 are OK since the constructor is used to instantiate <em><strong>\u201creal\u201d<\/strong><\/em> named objects. <span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<figure id=\"attachment_2287\" aria-describedby=\"caption-attachment-2287\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2287\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/example3-17\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3.jpg\" data-orig-size=\"952,279\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1610891490&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example3\" data-image-description=\"&lt;p&gt;Compiler Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Compiler Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3.jpg\" class=\"wp-image-2287\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3.jpg?resize=550%2C161&#038;ssl=1\" alt=\"Compiler Output\" width=\"550\" height=\"161\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3.jpg?resize=300%2C88&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3.jpg?resize=768%2C225&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example3.jpg?w=952&amp;ssl=1 952w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2287\" class=\"wp-caption-text\"><strong>Compiler Output<\/strong><\/figcaption><\/figure>\n<h4>Case-4: A function is declared [[nodiscard]]<\/h4>\n<p>In this case, instead of a data type (enum\/struct\/class), a specific <em><strong>function<\/strong><\/em> is marked <em><strong>[[nodiscard]<\/strong><\/em>]. See the following example.<\/p>\n<figure id=\"attachment_2289\" aria-describedby=\"caption-attachment-2289\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4-src.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2289\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/example4-src\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4-src.jpg\" data-orig-size=\"656,413\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1610892486&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Case-4: Function\" data-image-description=\"&lt;p&gt;Case-4: Function&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Case-4: Function&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4-src.jpg\" class=\"wp-image-2289\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4-src.jpg?resize=550%2C346&#038;ssl=1\" alt=\"Case-4: Function\" width=\"550\" height=\"346\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4-src.jpg?resize=300%2C189&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4-src.jpg?w=656&amp;ssl=1 656w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2289\" class=\"wp-caption-text\"><strong>Case-4: Function<\/strong><\/figcaption><\/figure>\n<p>Here, the <em><strong>\u201ccomputeSum()\u201d<\/strong><\/em> function takes two arguments and returns a result based on the arguments. It is only natural that we are expected to use the returned value. So, this function is declared <em><strong>[[nodiscard]]<\/strong><\/em>. However, in Line 93, the return value is ignored, triggering a warning, as expected. Line 96, because it uses the return value, is OK.<\/p>\n<figure id=\"attachment_2290\" aria-describedby=\"caption-attachment-2290\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2290\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/example4-12\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4.jpg\" data-orig-size=\"1156,275\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1610892431&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example4\" data-image-description=\"&lt;p&gt;Compiler Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Compiler Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4-1024x244.jpg\" class=\"wp-image-2290\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4.jpg?resize=550%2C131&#038;ssl=1\" alt=\"Compiler Output\" width=\"550\" height=\"131\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4.jpg?resize=300%2C71&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4.jpg?resize=1024%2C244&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4.jpg?resize=768%2C183&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example4.jpg?w=1156&amp;ssl=1 1156w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2290\" class=\"wp-caption-text\"><strong>Compiler Output<\/strong><\/figcaption><\/figure>\n<p>This completes the different use cases for <em><strong>[[nodiscard]]<\/strong><\/em>.<\/p>\n<h4>General Question: Should functions return a value?<\/h4>\n<p>Since the <em><strong>[[nodiscard]]<\/strong><\/em> attribute is about using\/ignoring the return value of functions, it makes sense to ask this general question. As most of us know, this is a <em><strong>\u201cdesign\u201d<\/strong><\/em> question and not a mere <em><strong>\u201cimplementation\u201d<\/strong><\/em> issue.<\/p>\n<p>Functions return a value for one of three reasons:<\/p>\n<p><span style=\"color: #0000ff;\"><em><strong>a) The caller depends on the return value and is expected to use it. <\/strong><\/em><\/span><\/p>\n<p style=\"padding-left: 40px;\"><em><strong>void * malloc()<\/strong><\/em><\/p>\n<p style=\"padding-left: 40px;\"><em><strong>int std::rand()<\/strong><\/em><\/p>\n<p style=\"padding-left: 40px;\"><em><strong>float std::floor(float arg)<\/strong><\/em><\/p>\n<p>In all the above cases, we call the function only for its return value. So is it not an error to ignore the return value? Such functions need to be declared <em><strong>[[nodiscard]]<\/strong><\/em>.<\/p>\n<p><span style=\"color: #0000ff;\"><em><strong>b) The caller &#8220;might&#8221; use the return value as &#8220;auxiliary&#8221; information. It is not essential for further computation and hence could be ignored. <\/strong><\/em><\/span><\/p>\n<p style=\"padding-left: 40px;\"><em><strong>int std::printf(const char *format, &#8230;)<\/strong><\/em><\/p>\n<p>Here the return value indicates the number of characters written, or if negative, indicates an error. Most of the time, the return value is ignored.<span class=\"Apple-converted-space\">\u00a0These functions should not be marked <em><strong>[[nodiscard]]<\/strong><\/em>.<\/span><\/p>\n<p><span style=\"color: #0000ff;\"><strong><em>c) The return value adheres to a coding convention and is intended to be used liberally, though not always. Consider the design of &#8220;fluent&#8221; <\/em>APIs.<\/strong><\/span><span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p style=\"padding-left: 40px;\"><em><strong>MyObject obj;<\/strong><\/em><\/p>\n<p style=\"padding-left: 40px;\"><em><strong>Obj.doThis().doThat(30).doSomethingElse(\u201cHello\u201d);<\/strong><\/em><\/p>\n<p>The above could also be written this way:<\/p>\n<p style=\"padding-left: 40px;\"><em><strong>MyObject obj;<\/strong><\/em><\/p>\n<p style=\"padding-left: 40px;\"><em><strong>Obj.doThis();<\/strong><\/em><\/p>\n<p style=\"padding-left: 40px;\"><em><strong>Obj.doThat(30);<\/strong><\/em><\/p>\n<p style=\"padding-left: 40px;\"><em><strong>Obj.doSomethingElse(\u201cHello\u201d);<\/strong><\/em><\/p>\n<p>Here, the use of return value promotes a stylistic convention, and is not a rigid rule. Such functions should not be marked <em><strong>[[nodiscard]]<\/strong><\/em> (in my opinion).<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Whether to declare an enum\/class\/struct\/function as <em><strong>[[nodiscard]]<\/strong><\/em> requires careful deliberation, and once decided, C++ allows us to explicitly declare our intention. This is a useful feature and we should start using this in our code.<\/p>\n<p>That is it for now. Hope you found the above discussion informative. You can download the sample code from <a href=\"http:\/\/www.rangakrish.com\/downloads\/NoDiscardExample.cpp\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Take care and have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[[nodiscard]]\u00a0attribute \u201cencourages\u201d the compiler to issue a warning when the return value from a function is ignored. It was introduced in C++ 17 and enhanced in C++ 20 to include a string literal that can be used as an explanation of the warning. Let us look at different cases one by one. Case-1: An enumeration [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[49,17],"tags":[67,262],"class_list":["post-2280","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c","tag-nodiscard"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-AM","jetpack-related-posts":[{"id":2850,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/26\/c20-no_unique_address-attribute\/","url_meta":{"origin":2280,"position":0},"title":"C++20 [[no_unique_address]] Attribute","author":"admin","date":"June 26, 2022","format":false,"excerpt":"The [[no_unique_address]] attribute was introduced in C++20 to give a compiler the freedom to optimise memory allocation of a struct\/class when it contains a subobject that does not have any members. The other requirement is that the subobject should not be a static member of the enclosing struct\/class. Let us\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Empty vs. Non-empty Struct","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1-300x213.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1-300x213.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1-300x213.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3675,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/04\/24\/interfaces-without-inheritance-comparing-c-and-common-lisp\/","url_meta":{"origin":2280,"position":1},"title":"Interfaces Without Inheritance: Comparing C++ and Common Lisp","author":"admin","date":"April 24, 2025","format":false,"excerpt":"Clean interface design is a crucial aspect of software engineering since it enables code flexibility, reuse, and maintainability. Developers who prefer an object-oriented approach typically rely on inheritance to define the interface and thus establish type relationships. While this can lead to a good design if approached carefully, detractors of\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Rectangle and Circle Defined","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/04\/cpp2-251x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":739,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/","url_meta":{"origin":2280,"position":2},"title":"C++17: constexpr if","author":"admin","date":"December 17, 2017","format":false,"excerpt":"constexpr if is another nice little feature in C++17, which significantly simplifies the way we code, especially in the context of templates. In this article, I will briefly talk about this feature and look at some examples. constexpr if is a variant of the standard if statement we have had\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1-1.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":198,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/04\/08\/c14-return-type-deduction-for-normal-functions\/","url_meta":{"origin":2280,"position":3},"title":"C++14: Return Type Deduction for Normal Functions","author":"admin","date":"April 8, 2016","format":false,"excerpt":"In C++11, a function must specify its return type in one of the following two ways: int aFunction(int arg) { \u00a0 \u00a0 return arg * 2; } (or) auto aFunction(int arg) -> int { \u00a0 \u00a0 return arg * 2; } The first is the classic function definition syntax. The\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2540,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/09\/19\/c-20-concepts\/","url_meta":{"origin":2280,"position":4},"title":"C++ 20: Concepts","author":"admin","date":"September 19, 2021","format":false,"excerpt":"Concepts, introduced in C++20, are predicates that act as contraints on template parameters. As you would expect, the nice thing is that the constraint checking happens as part of template instantiation at compile time and not at run time! Since templates can have type as well as non-type parameters, Concepts\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Constraint on Type","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/09\/Example1-300x213.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2722,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/03\/06\/lazy-parameter-evaluation-in-d-language\/","url_meta":{"origin":2280,"position":5},"title":"Lazy Parameter Evaluation in D Language","author":"admin","date":"March 6, 2022","format":false,"excerpt":"Languages such as C\/C++ and Java follow an\u00a0\"eager\" argument evaluation approach, where function arguments are evaluated before the function is entered. Of course, there are idioms and tricks that one can use to force \"lazy\" evaluation. The primary benefit of \"lazy\" parameter evaluation is that the parameter is evaluated only\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Example Program","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/eagercode-237x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2280"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2280\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}