Trick C++ on Unused Variables

If you enable warning on unused method parameters ( -Wextra or -Wunused) and you get warnings in method parameters, you’re supposed to change the signature. Sometimes this is not OK and you still need the build to pass cleanly.

Java has the glorious @SuppressWarnings, so my best and most portable solution is to define a macro:

Then, you use it in the body of your function like this:

This will trick the compiler to think the variable is used and it’ll be optimised out as well.


A little experiment: If you find this post and ad below useful, please check the ad out :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top