Trick C++ on Unused Variables

Posted by in Software

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 🙂