summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Fattributes%2Fmaybe_unused.html
blob: e8af27c7cb58e6e5301a722a1d4cf7c49aee9893 (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
    <h1 id="firstHeading" class="firstHeading">C attribute: maybe_unused <span class="t-mark-rev t-since-c23">(since C23)</span>
</h1>            <p>Suppresses warnings on unused entities.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin">  <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <code>[[</code> <code>maybe_unused</code> <code>]]</code><br><code>[[</code> <code>__maybe_unused__</code> <code>]]</code> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
</tr> 
</table> <h3 id="Explanation"> Explanation</h3> <p>This attribute can appear in the declaration of the following entities:</p>
<ul>
<li> <a href="../struct" title="c/language/struct">struct</a>/<a href="../union" title="c/language/union">union</a>: <code>struct [[maybe_unused]] S;</code>, </li>
<li> <a href="../typedef" title="c/language/typedef">typedef name</a>: <code>[[maybe_unused]] typedef S* PS;</code>, </li>
<li> object: <code>[[maybe_unused]] int x;</code>, </li>
<li> struct/union member: <code>union U { [[maybe_unused]] int n; };</code>, </li>
<li> <a href="../function_definition" title="c/language/function definition">function</a>: <code>[[maybe_unused]] void f(void);</code>, </li>
<li> <a href="../enum" title="c/language/enum">enumeration</a>: <code>enum [[maybe_unused]] E {};</code>, </li>
<li> enumerator: <code>enum { A [[maybe_unused]], B [[maybe_unused]] = 42 };</code>. </li>
</ul> <p>If the compiler issues warnings on unused entities, that warning is suppressed for any entity declared <code>maybe_unused</code>.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;assert.h&gt;
 
[[maybe_unused]] void f([[maybe_unused]] _Bool cond1, [[maybe_unused]] _Bool cond2)
{
   [[maybe_unused]] _Bool b = cond1 &amp;&amp; cond2;
   assert(b); // in release mode, assert is compiled out, and b is unused
              // no warning because it is declared [[maybe_unused]]
} // parameters cond1 and cond2 are not used, no warning
 
int main(void)
{
    f(1, 1);
}</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/maybe_unused" title="cpp/language/attributes/maybe unused">C++ documentation</a></span> for <code>maybe_unused</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/maybe_unused" class="_attribution-link">https://en.cppreference.com/w/c/language/attributes/maybe_unused</a>
  </p>
</div>