3 ms·
From my own recollection, the construction (void) var; for unused variables is specifically to silence certain versions of lint which would otherwise comp
by msla 3y ago
From my own recollection, the construction
(void) var;
for unused variables is specifically to silence certain versions of lint which would otherwise complain.
- kevin_thibedeau 3y agoNowadays it silences -Wunused-perameter. I'm not a fan as it just adds nonfunctional clutter only needed to appease a tool. The tools are supposed to work on our behalf, not against.
- loup-vaillant 3y agoWe can also silence -Wunused-perameter by omitting the parameter name in the prototype. No clutter, I'm personally fairly happy with this solution. (Note: the declaration in the header can sill name the parameter, it won't bring the warning back.)
- ludocode 3y agoThat's only in C++. As far as I know in C you need to name all parameters (although maybe that rule has been relaxed in newer versions of C.)
- flohofwoe 3y agoC23 finally relaxes that rule: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2510.pdf https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2510.pdf (although some C compilers already allowed it in non-pedantic mode)
- flohofwoe 3y agoI much prefer being warned about unused variables and explicitely having to silence those warnings on a case by case basis though - especially when refactoring (one option is to use a wrapper macro MAYBE_UNUSED(bla) macro, or GCC's __attribute__((unused)) or the C++ [[unused]], or the C23 [[maybe_unused]]. But of all those options, "(void)bla;" might actually be the least clutter (and at the same time the most compatible).
- mtklein 3y agoI agree that (void)bla is desirably clear, pithy and unambiguous. And I like something like this if I'm dealing with functions with more than a couple unused parameters: static void unused_(int x, ...) { (void)x; } #define unused(...) unused_(0, __VA_ARGS__) This way you can just have one `unused(foo,bar,baz,quux);` line at the top of the function listing all unused parameters.