Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/attributes/maybe unused"

From cppreference.com
< cpp‎ | language‎ | attributes
m (References: Consistent order: newest to oldest)
 
(16 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{cpp/attribute/title|maybe_unused|notes={{mark since c++17}}}}
+
{{cpp/language/attributes/title|maybe_unused|notes={{mark since c++17}}}}
{{cpp/attribute/navbar}}
+
{{cpp/language/attributes/navbar}}
  
If the compiler issues warnings on unused entities, that warning is suppressed for any entity declared maybe_unused.
+
Suppresses warnings on unused entities.
  
 
===Syntax===
 
===Syntax===
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|1=
+
{{sdsc|1=
{{spar|class-key}} {{ttb|<nowiki>[[</nowiki>maybe_unused<nowiki>]]</nowiki>}} {{spar|class-name}} {{ttb|;}}
+
{{ttb|{{c/core|[[maybe_unused]]}}}}
}}
+
{{sdsc|num=2|1=
+
{{spar|class-key}} {{ttb|<nowiki>[[</nowiki>maybe_unused<nowiki>]]</nowiki>}} {{spar|class-name}} {{spar|final}}{{mark optional}} {{spar|base-clause}}{{mark optional}} {{ttb|{}} {{spar|member-specification}} {{ttb|} }}
+
}}
+
{{sdsc|num=3|1=
+
{{ttb|using}} {{spar|identifier}} {{ttb|<nowiki>[[</nowiki>maybe_unused<nowiki>]]</nowiki>}} {{ttb|{{=}}}} {{spar|defining-type-id}} {{ttb|;}}
+
}}
+
{{sdsc|num=4|1=
+
{{ttb|<nowiki>[[</nowiki>maybe_unused<nowiki>]]</nowiki>}} {{spar|declaration}}
+
}}
+
{{sdsc|num=5|1=
+
{{spar|declarator-id}} {{ttb|<nowiki>[[</nowiki>maybe_unused<nowiki>]]</nowiki>}}
+
}}
+
{{sdsc|num=6|1=
+
{{spar|enum-key}} {{ttb|<nowiki>[[</nowiki>maybe_unused<nowiki>]]</nowiki>}} {{spar|enumeration-name}} {{spar|enum-base}}{{mark optional}} {{ttb|;}}
+
}}
+
{{sdsc|num=7|1=
+
{{spar|enum-key}} {{ttb|<nowiki>[[</nowiki>maybe_unused<nowiki>]]</nowiki>}} {{spar|enumeration-name}} {{spar|enum-base}}{{mark optional}} {{ttb|{}} {{spar|enumerator-list}} {{ttb|} }}
+
}}
+
{{sdsc|num=8|1=
+
{{spar|enumerator}} {{ttb|<nowiki>[[</nowiki>maybe_unused<nowiki>]]</nowiki>}}
+
 
}}
 
}}
 
{{sdsc end}}
 
{{sdsc end}}
  
 
===Explanation===
 
===Explanation===
Appears in the declaration of a class, a typedef­, a variable, a non­static data member, a function, an enumeration, or an enumerator.
+
This attribute can appear in the declaration of the following entities:
 +
* [[cpp/language/classes|class/struct/union]]: {{c|struct [[maybe_unused]] S;}}
 +
* {{lt|cpp/language/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;}}
 +
* [[cpp/language/data members|non-static data member]]: {{c|union U { [[maybe_unused]] int n; };}},
 +
* {{lt|cpp/language/function}}: {{c|[[maybe_unused]] void f();}}
 +
* [[cpp/language/enum|enumeration]]: {{c|enum [[maybe_unused]] E {};}}
 +
* enumerator: {{c|1=enum { A [[maybe_unused]], B [[maybe_unused]] = 42 };}}
 +
* {{lt|cpp/language/structured binding}}: {{c|1=[[maybe_unused]] auto [a, b] = std::make_pair(42, 0.23);}}
  
If the compiler issues warnings on unused entities, that warning is suppressed for any entity declared maybe_unused.  
+
For entities declared {{c|[[maybe_unused]]}}, if the entities or their structured bindings are unused, the warning on unused entities issued by the compiler is suppressed.
 +
 
 +
{{rrev|since=c++26|
 +
For labels declared {{c|[[maybe_unused]]}}, if they are unused, the warning on unused labels issued by the compiler is suppressed.
 +
}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
 
|code=
 
|code=
 +
#include <cassert>
 +
 
[[maybe_unused]] void f([[maybe_unused]] bool thing1,
 
[[maybe_unused]] void f([[maybe_unused]] bool thing1,
 
                         [[maybe_unused]] bool thing2)
 
                         [[maybe_unused]] bool thing2)
 
{
 
{
  [[maybe_unused]] bool b = thing1 && thing2;
+
    [[maybe_unused]] lb: // the label “lb” is not used, no warning
  assert(b); // in release mode, assert is compiled out, and b is unused
+
    [[maybe_unused]] bool b = thing1 && thing2;
              // no warning because it is declared [[maybe_unused]]
+
    assert(b); // in release mode, assert is compiled out, and “b” is unused
} // parameters thing1 and thing2 are not used, no warning
+
              // no warning because it is declared [[maybe_unused]]
 +
} // parameters “thing1” and “thing2” are not used, no warning
 +
 
 +
int main() {}
 
}}
 
}}
  
{{langlinks|ja|zh}}
+
===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}}
 +
 
 +
===References===
 +
{{ref std c++23}}
 +
{{ref std|section=9.12.8|title=Maybe unused attribute|id=dcl.attr.unused}}
 +
{{ref std end}}
 +
{{ref std c++20}}
 +
{{ref std|section=9.12.7|title=Maybe unused attribute|id=dcl.attr.unused}}
 +
{{ref std end}}
 +
{{ref std c++17}}
 +
{{ref std|section=10.6.6|title=Maybe unused attribute|id=dcl.attr.unused}}
 +
{{ref std end}}
 +
 
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc see c|c/language/attributes/maybe_unused}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|es|ja|zh}}

Latest revision as of 11:44, 12 August 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 
Attributes
(C++23)
(C++14)
(C++20)
maybe_unused
(C++17)
(C++17)
(C++11)
(C++20)
 

Suppresses warnings on unused entities.

Contents

[edit] Syntax

[[maybe_unused]]

[edit] Explanation

This attribute can appear in the declaration of the following entities:

For entities declared [[maybe_unused]], if the entities or their structured bindings are unused, the warning on unused entities issued by the compiler is suppressed.

For labels declared [[maybe_unused]], if they are unused, the warning on unused labels issued by the compiler is suppressed.

(since C++26)

[edit] Example

#include <cassert>
 
[[maybe_unused]] void f([[maybe_unused]] bool thing1,
                        [[maybe_unused]] bool thing2)
{
    [[maybe_unused]] lb: // the label “lb” is not used, no warning
    [[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() {}

[edit] 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

[edit] References

  • C++23 standard (ISO/IEC 14882:2024):
  • 9.12.8 Maybe unused attribute [dcl.attr.unused]
  • C++20 standard (ISO/IEC 14882:2020):
  • 9.12.7 Maybe unused attribute [dcl.attr.unused]
  • C++17 standard (ISO/IEC 14882:2017):
  • 10.6.6 Maybe unused attribute [dcl.attr.unused]

[edit] See also

C documentation for maybe_unused