Topic: volatile vs. assignment


Author: pasa@lib.hu ("Balog Pal")
Date: Fri, 20 Feb 2004 20:05:06 +0000 (UTC)
Raw View
on clcm I read a claim that conforming to the standard having a volatile
object, assignment to it will have a lvalue->rvalue conversion at the end
that shall not be discarded. Thus

extern volatile int v;

v=1;

will emit code with a write to the location, *and then a read access* to
that same location.  What may lead to unexpected events if v is some memory
mapped i/o register.

A recent post counters it:
- ---------
"Ben Hutchings" <do-not-spam-benh@bwsint.com> wrote in message
news:slrnc372a0.p3b.do-not-spam-benh@shadbolt.i.decadentplace.org.uk...
(on clc++m)
> Jack Klein wrote:
> <snip>
>  > Note that under the current C++ standard, assigning to a volatile
>  > object yields an lvalue result.  An lvalue in an expression calls for
>  > an lvalue-to-rvalue conversion in the abstract machine, and can
>  > normally be optimized away if the rvalue is not used.
> <snip>
>
> The lvalue-to-rvalue conversion is done in "a context where an rvalue
> is expected" (3.10/7), e.g. where the lvalue appears as a sub-
> expression, initialiser or return statement which requires an rvalue.
> An expression statement is not such a context (6.2/1), so
>
>      b = a;
>
> requires lvalue-to-rvalue conversion on a but not on b.  However,
>
>      c = b = a;
>
> requires lvalue-to-rvalue conversion on both a and b but not c.
- -------------------

What is the truth in this question -- is that read mandatory, optional or
forbidden?

Paul


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]