The question is, what should C and C++ compilers do with this code?
volatile int x;
void foo (void) {
x;
}
This question is not totally stupid: embedded systems use code like this
when reading a hardware register has a useful side effect (usually
clearing the register).
It is reasonably clear that a C compiler should load from x and throw
away the value. gcc does this, as do most decent C compilers.
However, g++ also loads from x and this does not appear to be supported
by the 1998 C++ standard. In 6.2, it is explicitly stated that for an
expression statement, no conversion from lvalue to rvalue occurs. If
there's no rvalue, there should not be a load from x.
Anyway, I'm curious: is the C-like interpretation of a volatile
expression statement considered to be a feature by the g++ maintainers?
If so, what is the rationale?
I haven't do extensive testing, but there do exist compiler families
(such as those from IAR and Intel) where the C compiler loads from x and
the C++ compiler does not.
Thanks,
John Regehr