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

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
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
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
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.
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.
Thanks! Personal prefs ... I would just say i-1 if j= i-1. Doesn't make any real difference.
Oh, for goodness sake, read their bio's.

Spam doesn't just appear in posts.
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
> 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.
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.