Why does C++ code missing a formal argument name in a function definition compile without warnings?

Because sometimes you have a parameter that’s required by an interface but the function doesn’t use it. Maybe the parameter is no longer necessary, is only necessary in other functions that must use the same signature (especially so they can be called through pointers) or the functionality hasn’t been implemented yet. Having parameters that aren’t used can be particularly common in generated or framework code for this reason (and that’s probably why the MFC generated code has the name commented out).

As to why there’s no warning – I guess it’s because whether this is a problem is a subjective thing and other people (particularly compiler implementers) don’t see it as a problem. Once you actually go to use the parameter, you’ll get the compiler to complain if you forget to uncomment the name so you get the compiler complaining only when you really need it to (the compiler’s version of the agile YAGNI: “You Aren’t Gonna Neet It” philosophy).

The opposite does seem to generally occur when you crank up warnings – named parameters that aren’t used generate warnings – again that’s probably why the generated function has the name commented out.

Leave a Comment