If I call a function which has a volatile parameter, and that parameter is not used, must the compiler nonetheless produce the parameter?
void consume( volatile int ) { }
...
consume( some_expr );
GCC does honour this, but I’m uncertain if the wording of volatile in the standards requires this. In my opinion GCC is doing the right thing — this is logically an assignment to a volatile variable and thus should not be omitted (according to 1.9-8 of the c++ standard)
NOTE: The purpose of this is to prevent the optimizer from removing the evaluation of code. That is, it forces some_expr to be evaluated. It allows the expression to be optimized, but ensures it is actually performed.
I’ve add C and C++ as tags as the answer to both interests me should there be any differences. I don’t think there would be however.
ANSWER: I’ve picked the first one as I believe it is the correct practical realization of the standard. However, Steve’s philosophical viewpoint is very interesting and may in fact mean the standard is ambiguous.
The unnamed argument of
consumecannot be read, since it’s unnamed. However, it is initialized, and that initialization (withsome_expr) is a visible side-effect. Therefore the compiler may not optimize the initialization out.Whether this requires the actual evalution of
some_expris another matter. In general that is not a visible side-effect, but it could be ifsome_exprcontainsvolatilesub-expressions.[edit]
Please note that the “unnamed” part can occur in two places. The caller in general has no way to know whether a parameter is named (let alone used) E.g.