Let's say I have a class with a virtual function with one argument and two different implementations of this virtual function. The first implementation uses the argument while the second does not. The second case will produce a compilation warning. There are two ways I can think of to suppress the warning.
- Using an anonymous parameter.
- Using the
[[maybe_unused]]
annotation.
What is considered "best practice" between the two?
Thank you in advance for your answers.
CodePudding user response:
The most authoritative source we have for "best practice" in C is the C Core Guidelines. And on the topic of unused arguments, they have this to say
F.9: Unused parameters should be unnamed
...
If parameters are conditionally unused, declare them with the
[[maybe_unused]]
attribute.
So the C Core Guidelines recommend leaving the parameter unnamed if it's never used at all. [[maybe_unused]]
, by this rule, should only be applied if the parameter is unused in some situations, such as inside an #ifdef
or (more modern) inside an if constexpr
.