On 03/06/2010 04:43 AM, John Regehr wrote:
> 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.

I'm not sure this follows.  It's stated explicitly that "The
expression is evaluated and its value is discarded."  How can you
evaluate the expression without reading the volatle?

Also, there's the non-normative [Note: volatile is a hint to the
implementation to avoid aggressive optimization involving the object
because the value of the object might be changed by means undetectable
by an implementation. See 1.9 for detailed semantics. In general, the
semantics of volatile are intended to be the same in C++ as they are
in C. ]

Besides, all that changing this would do is break programs.

Andrew.

Reply via email to