Possible error "sequenced before" and volatile in C++0x

  • Thread starter Johannes Schaub (litb)
  • Start date
J

Johannes Schaub (litb)

C++0x says at [intro.execution]/15

"If a side effect on a scalar object is unsequenced relative to either
another side effect on the same scalar object or a value computation using
the value of the same scalar object, the behavior is undefined."

That does not allow reading any volatile variable:

int volatile a = 0;

// undefined behavior:
// - value computation of a
// - side effect on a
// They seem to be unsequenced!
int b = a;

Can anyone please show how these two are sequenced so we do not have
undefined behavior? 4.1 lvalue-to-rvalue does not contain anything to that
effect it seems.
 
P

Pavel

Johannes said:
C++0x says at [intro.execution]/15

"If a side effect on a scalar object is unsequenced relative to either
another side effect on the same scalar object or a value computation using
the value of the same scalar object, the behavior is undefined."

That does not allow reading any volatile variable:

int volatile a = 0;

// undefined behavior:
// - value computation of a
// - side effect on a
// They seem to be unsequenced!
int b = a;
Can anyone please show how these two are sequenced so we do not have
undefined behavior? 4.1 lvalue-to-rvalue does not contain anything to that
effect it seems.

IMHO there is a terminology defect in the draft here:

1.9-12:
.... When a call
to a library I/O function returns *or an access to a volatile object* is
evaluated the side effect is considered complete...

The notion of "complete side effect" introduced here does not seem to be
used anywhere else; if so it is useless unless we assume the authors
actually meant the side effect is sequenced before the end of evaluation
of access to a volatile object.

-Pavel
 
J

Johannes Schaub (litb)

Johannes said:
C++0x says at [intro.execution]/15

"If a side effect on a scalar object is unsequenced relative to either
another side effect on the same scalar object or a value computation using
the value of the same scalar object, the behavior is undefined."

That does not allow reading any volatile variable:

int volatile a = 0;

// undefined behavior:
// - value computation of a
// - side effect on a
// They seem to be unsequenced!
int b = a;

Can anyone please show how these two are sequenced so we do not have
undefined behavior? 4.1 lvalue-to-rvalue does not contain anything to that
effect it seems.

What i *think* is needed here is to sequence the side effect on "a" before
the value-computation on "a". Tweaking 4.1 to say the following could
suffice, I believe

"... Otherwise, the value contained in the object indicated by the glvalue
is the prvalue result. If the glvalue has a volatile qualified type, any
side-effect induced on it is sequenced before value computation of the
prvalue result."

This guarantees that any external change to a volatile variable prior to a
read of it is observed by that read.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top