Suppresses warnings on unused entities.
[[maybe_unused]] |
This attribute can appear in the declaration of the following entities:
struct [[maybe_unused]] S;
, [[maybe_unused]] typedef S* PS;
, using PS [[maybe_unused]] = S*;
, [[maybe_unused]] int x;
, union U { [[maybe_unused]] int n; };
, [[maybe_unused]] void f();
, enum [[maybe_unused]] E {};
, enum { A [[maybe_unused]], B [[maybe_unused]] = 42 };
, [[maybe_unused]] auto [a, b] = std::make_pair(42, 0.23);
. 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.
#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() {}
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 |
C documentation for maybe_unused |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/language/attributes/maybe_unused