By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,636 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

Sequence point problem?

P: n/a
Hello all,

Can anybody identify any sequence point problems in the expression below?

i = (i == string::npos) ? 0 : ++i;

Thanks,
Dave
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Dave" <be***********@yahoo.com> wrote...
Can anybody identify any sequence point problems in the expression below?

i = (i == string::npos) ? 0 : ++i;


The Standard says that between sequence points (and there are none in
this expression) a stored value of any object shall be modified at most
once. If the expression in the parentheses yields false, 'i' will be
modified twice, once by ++ and the other time with the assignment, which
causes undefined behaviour. If you rewrite this as

i = (i == string::npos) ? 0 : i + 1;

there would be no problem.

V
Jul 23 '05 #2

P: n/a
On Sat, 5 Feb 2005 15:31:49 -0500, "Victor Bazarov"
<v.********@comAcast.net> wrote in comp.lang.c++:
"Dave" <be***********@yahoo.com> wrote...
Can anybody identify any sequence point problems in the expression below?

i = (i == string::npos) ? 0 : ++i;


The Standard says that between sequence points (and there are none in
this expression) a stored value of any object shall be modified at most
once. If the expression in the parentheses yields false, 'i' will be
modified twice, once by ++ and the other time with the assignment, which
causes undefined behaviour. If you rewrite this as

i = (i == string::npos) ? 0 : i + 1;

there would be no problem.


There is a sequence point in this expression, at the '?' after the
condition has been evaluated but before the evaluation of the chosen
predicate expression.

But your reasoning and correction are right.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #3

P: n/a
Jack Klein wrote:
On Sat, 5 Feb 2005 15:31:49 -0500, "Victor Bazarov"
<v.********@comAcast.net> wrote in comp.lang.c++:

"Dave" <be***********@yahoo.com> wrote...
Can anybody identify any sequence point problems in the expression below?

i = (i == string::npos) ? 0 : ++i;


The Standard says that between sequence points (and there are none in
this expression) a stored value of any object shall be modified at most
once. If the expression in the parentheses yields false, 'i' will be
modified twice, once by ++ and the other time with the assignment, which
causes undefined behaviour. If you rewrite this as

i = (i == string::npos) ? 0 : i + 1;

there would be no problem.

There is a sequence point in this expression, at the '?' after the
condition has been evaluated but before the evaluation of the chosen
predicate expression.


Thank you for the correction, Jack. So, even though there is a SeqP in
that expression, ++i would be evaluated *after* it, right? Which still
makes this essentially the same as

if (i == string::npos)
i = 0;
else
i = ++i; // undefined behaviour

V
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.