diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/language%2Fattributes%2Fnodiscard.html | |
new repository
Diffstat (limited to 'devdocs/c/language%2Fattributes%2Fnodiscard.html')
| -rw-r--r-- | devdocs/c/language%2Fattributes%2Fnodiscard.html | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fattributes%2Fnodiscard.html b/devdocs/c/language%2Fattributes%2Fnodiscard.html new file mode 100644 index 00000000..35c94f7d --- /dev/null +++ b/devdocs/c/language%2Fattributes%2Fnodiscard.html @@ -0,0 +1,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 &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"> + © 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> |
