Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

Comma in CPP

This topic is 4262 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
I haven't used it much in this way (readability concerns in 90% of cases), but the comma operator basically allows multiple statements to be executed in order where one statement is expected.

so i = 1, i++; is literally executed as:
1: i = 1;
2: i++;

watch your brackets as i = (1, i++); is different from i= 1, i++;

int i = 0;

i = (1, i++);

returns the result 1... But that's because it executes like this:

1;
i++;
i = i;

So in this case if you hadn't initialized i first it would actually be incrementing i without the assignment as in the first example which could lead to very costly errors depending on implementation. You can see how this makes the entire situation more complicated than it needs to be and why it's needlessly obscure (not a great word for it, but meh) in this case.

Basically that's it.

Just be very cautious and only use it where necessary, these are contrived examples. Know that.

Share this post


Link to post
Share on other sites
It is valid. But not good, don't do it.

things such as arguments don't work.

cout << 1,2 << endl;

is invalid syntactically because there are two values where one is expected, however (1,2) is fine because it returns a single value after being evaluated... In fact, the last value mentioned (2 in this case).

In your example of

(1, i) = 1;

the last value, i is chosen for the assignment, if you swap the values you'll have problems assigning a value to a symbol or a const or whatever you want to call it.

Share this post


Link to post
Share on other sites
Quote:
Original post by M2tM
[about i = (1, i++);]

1;
i++;
i = i;


Note that it may also execute as:

1;
int temp = i;
i++;
i = temp;

The order of execution between the = and the ++ operators is unspecified, therefore the code here is non-deterministic.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by M2tM
[about i = (1, i++);]

1;
i++;
i = i;


Note that it may also execute as:

1;
int temp = i;
i++;
i = temp;

The order of execution between the = and the ++ operators is unspecified, therefore the code here is non-deterministic.


Indeed, it may work on some configurations, it may not. Don't do it.

Thanks for the extra info, and yes, it would be the same problem as i = i++;

Now I have a question though, and it's a simple one but one I just haven't really looked into. Do brackets enable this to be safe?

i = (i++);

My gut reaction is no, especially if a temporary storage space is used exactly as you have there.

The other thing playing in my mind is that maybe the brackets ensure that the operation completes and somehow returns the complete value. I don't know if this is defined in the standards, I'd check it in google but I don't know what I'd search for.

*edit: It seems it goes based on something called "sequence points" I'll look that up, but feel free to supply a description or links.

Share this post


Link to post
Share on other sites
I find using the comma operator to be very useful in for loops:

for(int y=0, index=0 ; y<height ; ++y)
for(int x=0 ; x<width ; ++y, ++index)
buffer = calcSomething(x,y);

Share this post


Link to post
Share on other sites
Yeah, for loops are pretty much the best place for them.

sequence points

There we go... So no, i = (i++); is still undefined as I thought...

The () don't trigger a sequence point.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!