order of execution in an expression with commas in C++

Jul 15, 2019 at 9:28am
It is my understanding that the term j = i will be executed before ++i in the statement

 
  j = i, ++i;


Does the C++ standard guarantee that j = i will be executed before ++i in the loop

 
for (auto i = std::next(begin), j = begin; i!= end; j= i, ++i)


???


https://www.mybkexperience.xyz/
Last edited on Jul 18, 2019 at 11:30am
Jul 15, 2019 at 9:32am
I'm pretty sure that it is Yes, but if you really want to be sure, you can do this :
 
for (auto i = std::next(begin), j = begin; i!= end; j= i++)
Last edited on Jul 15, 2019 at 2:00pm
Jul 15, 2019 at 12:56pm
The answer is yes, since the comma operator has the lowest precedence of all.
Also, it it's left side is "sequenced before" it's right side, so the fact that i is being referenced on one side and modified on the other is okay, too. https://en.cppreference.com/w/cpp/language/eval_order

However, @Zaap shows how it is usually done.
Last edited on Jul 15, 2019 at 1:03pm
Jul 15, 2019 at 1:36pm
I am still a little rough with the auto loops -- is this an idiom? Why 2 variables (it looks like you only need i or j, but not both, here?). Thanks.
Jul 15, 2019 at 1:46pm
It looks like j is meant to hold the "previous" iterator value (although it's not really initialized properly for that purpose).

I'm not use if I'd call this an "auto" loop. It's not a range-based for, if that's what you mean.
Jul 15, 2019 at 2:19pm
Thanks! Personal prefs ... I would just say i-1 if j= i-1. Doesn't make any real difference.
Jul 15, 2019 at 2:40pm
Oh, for goodness sake, read their bio's.

Spam doesn't just appear in posts.
Jul 15, 2019 at 2:56pm
Good catch. That's a new one to me. But it's a weak way to advertise. Almost no one will look.

Still, I'll check the bio of first-time posters from now on.
Last edited on Jul 15, 2019 at 2:57pm
Jul 15, 2019 at 6:12pm
> I would just say i-1
you may do that if `i' is a random access iterator (like the one from std::vector)
you can't do "jumps" with a bidirectional iterator (like the one from std::list)
and you can't go backwards with a forward iterator (like the one from std::forward_list)

> Oh, for goodness sake, read their bio's.
¿what? it's just a link to their home page...
(follows the link)
I see...
damn.

and nothing of that would have happened if you just kept your mouth shut.
Jul 15, 2019 at 10:23pm
No, the OP just grabbed a random post from S.O.
https://stackoverflow.com/questions/40773412/order-of-execution-in-an-expression-with-commas-in-c
Feel free to report and delete it.

The one that I was referring to has long been deleted by someone else.
Topic archived. No new replies allowed.