When you try to compile this code
int main()
{
int x = 0;
}
with gcc -Werror -Wall you get an error:
<source>: In function 'int main()':
<source>:3:9: error: unused variable 'x' [-Werror=unused-variable]
3 | int x = 0;
| ^
cc1plus: all warnings being treated as errors
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:3:9: error: unused variable 'x' [-Werror=unused-variable]
3 | int x = 0;
| ^
cc1plus: all warnings being treated as errors
The old-fashioned way to silence that warning (treated as error here) is to write
int main()
{
int x = 0;
(void)(x);
}
However, since C++17 there is the attribute [maybe_unused]
. Using that gcc does not produce an error with -Wall -Werror
for this:
int main()
{
[[maybe_unused]] int x = 0;
}