Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Best practice: [[maybe_unused]] or anonymous argument?

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.

  1. Using an anonymous parameter.
  2. Using the [[maybe_unused]] annotation.

What is considered "best practice" between the two?

Thank you in advance for your answers.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading