C++ attribute: maybe_unused (since C++17)

From cppreference.com
< cpp‎ | language‎ | attributes
 
 
 
 
Attributes
(C++14)
(C++17)
maybe_unused
(C++17)
(C++20)(C++20)
(C++20)(C++20)(C++20)
 

Suppresses warnings on unused entities.

Syntax

[[maybe_unused]]

Explanation

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

If the compiler issues warnings on unused entities, that warning is suppressed for any entity declared maybe_unused.

Example

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