summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Fattributes%2Fnodiscard.html
blob: 35c94f7d008a104250aef0efec8f0b7f17093018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    <h1 id="firstHeading" class="firstHeading">C attribute: nodiscard <span class="t-mark-rev t-since-c23">(since C23)</span>
</h1>            <p>If a function declared <code>nodiscard</code> or a function returning a struct/union/enum declared <code>nodiscard</code> by value is called from a <a href="../expressions" title="c/language/expressions">discarded-value expression</a> other than a cast to <code>void</code>, the compiler is encouraged to issue a warning.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin">  <tr class="t-sdsc"> <td> <code>[[</code> <code>nodiscard</code> <code>]]</code><br><code>[[</code> <code>__nodiscard__</code> <code>]]</code> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
</tr>  <tr class="t-sdsc"> <td> <code>[[</code> <code>nodiscard</code> <code>(</code> <span class="t-spar">string-literal</span> <code>)</code> <code>]]</code><br><code>[[</code> <code>__nodiscard__</code> <code>(</code> <span class="t-spar">string-literal</span> <code>)</code> <code>]]</code> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td>
</tr> 
</table> <table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">string-literal</span> </td> <td> - </td> <td> text that could be used to explain the rationale for why the result should not be discarded </td>
</tr>
</table> <h3 id="Explanation"> Explanation</h3> <p>Appears in a function declaration, enumeration declaration, or struct/union declaration.</p>
<p>If, from a <a href="../expressions" title="c/language/expressions">discarded-value expression</a> other than a cast to <code>void</code>,</p>
<ul>
<li> a function declared <code>nodiscard</code> is called, or </li>
<li> a function returning a struct/union/enum declared <code>nodiscard</code> is called, </li>
</ul> <p>the compiler is encouraged to issue a warning.</p>
<p>The <span class="t-spar">string-literal</span>, if specified, is usually included in the warnings.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">struct [[nodiscard]] error_info { int status; /*...*/ };
struct error_info enable_missile_safety_mode() { /*...*/ return (struct error_info){0}; }
void launch_missiles() { /*...*/ }
void test_missiles() {
   enable_missile_safety_mode(); // compiler may warn on discarding a nodiscard value
   launch_missiles();
}
struct error_info* foo() { static struct error_info e; /*...*/ return &amp;e; }
void f1() {
    foo(); // nodiscard type itself is not returned, no warning
}
// nodiscard( string-literal ):
[[nodiscard("PURE FUN")]] int strategic_value(int x, int y) { return x ^ y; }
 
int main()
{
    strategic_value(4,2); // compiler may warn on discarding a nodiscard value
    int z = strategic_value(0,0); // OK: return value is not discarded
    return z;
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">game.cpp:5:4: warning: ignoring return value of function declared with 'nodiscard' attribute
game.cpp:17:5: warning: ignoring return value of function declared with 'nodiscard' attribute: PURE FUN</pre></div> </div> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/attributes/nodiscard" title="cpp/language/attributes/nodiscard">C++ documentation</a></span> for <code>nodiscard</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/language/attributes/nodiscard" class="_attribution-link">https://en.cppreference.com/w/c/language/attributes/nodiscard</a>
  </p>
</div>