Difference between revisions of "cpp/language/attributes/maybe unused"
From cppreference.com
< cpp | language | attributes
m (→Example: runs silently.) |
(Added CWG issue #2360 DR.) |
||
Line 7: | Line 7: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|1= | {{sdsc|1= | ||
− | {{ttb| | + | {{ttb|{{c/core|[[maybe_unused]]}}}} |
}} | }} | ||
{{sdsc end}} | {{sdsc end}} | ||
Line 15: | Line 15: | ||
* [[cpp/language/classes|class/struct/union]]: {{c|struct [[maybe_unused]] S;}}, | * [[cpp/language/classes|class/struct/union]]: {{c|struct [[maybe_unused]] S;}}, | ||
− | * [[cpp/language/typedef|typedef]], including those declared by [[cpp/language/ | + | * [[cpp/language/typedef|typedef]], including those declared by [[cpp/language/type alias|alias declaration]]: {{c|[[maybe_unused]] typedef S* PS;}}, {{c|1=using PS [[maybe_unused]] = S*;}}, |
* variable, including [[cpp/language/static|static data member]]<!--clang used to get this wrong-->: {{c|[[maybe_unused]] int x;}}, | * variable, including [[cpp/language/static|static data member]]<!--clang used to get this wrong-->: {{c|[[maybe_unused]] int x;}}, | ||
− | * [[cpp/language/ | + | * [[cpp/language/data members|non-static data member]]: {{c|union U { [[maybe_unused]] int n; };}}, |
* [[cpp/language/function|function]]: {{c|[[maybe_unused]] void f();}}, | * [[cpp/language/function|function]]: {{c|[[maybe_unused]] void f();}}, | ||
* [[cpp/language/enum|enumeration]]: {{c|enum [[maybe_unused]] E {};}}, | * [[cpp/language/enum|enumeration]]: {{c|enum [[maybe_unused]] E {};}}, | ||
− | * enumerator: {{c|1=enum { A [[maybe_unused]], B [[maybe_unused]] = 42 };}} | + | * enumerator: {{c|1=enum { A [[maybe_unused]], B [[maybe_unused]] = 42 };}}, |
− | + | * [[cpp/language/structured binding|structured binding]]: {{c|1=[[maybe_unused]] auto [a, b] = std::make_pair(42, 0.23);}}. | |
− | + | ||
+ | For entites declared {{c|[[maybe_unused]]}}, if the entities or their structured bindings are unused, the warning on unused entities issued by the compiler is suppressed. | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
Line 32: | Line 32: | ||
[[maybe_unused]] bool thing2) | [[maybe_unused]] bool thing2) | ||
{ | { | ||
− | + | [[maybe_unused]] bool b = thing1 && thing2; | |
− | + | assert(b); // in release mode, assert is compiled out, and b is unused | |
− | + | // no warning because it is declared [[maybe_unused]] | |
} // parameters thing1 and thing2 are not used, no warning | } // parameters thing1 and thing2 are not used, no warning | ||
− | int main() { | + | int main() {} |
}} | }} | ||
+ | |||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|wg=cwg|dr=2360|std=C++17|before=could not apply {{c|[[maybe_unused]]}} to structured bindings|after=allowed}} | ||
+ | {{dr list end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc see c | c/language/attributes/maybe_unused}} | + | {{dsc see c|c/language/attributes/maybe_unused}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|es|ja|zh}} | {{langlinks|es|ja|zh}} |
Revision as of 19:10, 24 July 2022
Suppresses warnings on unused entities.
Contents |
Syntax
[[maybe_unused]]
|
|||||||||
Explanation
This attribute can appear in the declaration of the following entities:
- class/struct/union: struct [[maybe_unused]] S;,
- typedef, including those declared by alias declaration: [[maybe_unused]] typedef S* PS;, using PS [[maybe_unused]] = S*;,
- variable, including static data member: [[maybe_unused]] int x;,
- non-static data member: union U { [[maybe_unused]] int n; };,
- function: [[maybe_unused]] void f();,
- enumeration: enum [[maybe_unused]] E {};,
- enumerator: enum { A [[maybe_unused]], B [[maybe_unused]] = 42 };,
- structured binding: [[maybe_unused]] auto [a, b] = std::make_pair(42, 0.23);.
For entites declared [[maybe_unused]], if the entities or their structured bindings are unused, the warning on unused entities issued by the compiler is suppressed.
Example
Run this code
#include <cassert> [[maybe_unused]] void f([[maybe_unused]] bool thing1, [[maybe_unused]] bool thing2) { [[maybe_unused]] bool b = thing1 && thing2; assert(b); // in release mode, assert is compiled out, and b is unused // no warning because it is declared [[maybe_unused]] } // parameters thing1 and thing2 are not used, no warning int main() {}
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 2360 | C++17 | could not apply [[maybe_unused]] to structured bindings | allowed |
See also
C documentation for maybe_unused
|