Comma in CPP

Started by
33 comments, last by DaBookshah 17 years, 5 months ago
What is the difference between:

int i = 0;

i = 1, i++;
And:

int i = 0;

i = 1;
i++;
Thanks in advance.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
The former is less readable that the later. Other than that, there is no difference :)

Regards,
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.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
What about:
int i=0;(1, i) = 1;


Is this valid?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
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.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -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.

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.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
By the way, I am not going to "do it". I am just writting a small CPP like compiler.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
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);
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk

This topic is closed to new replies.

Advertisement