# Comma in CPP

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

## Recommended Posts

What is the difference between:
int i = 0;

i = 1, i++;

And:
int i = 0;

i = 1;
i++;


##### Share on other sites
The former is less readable that the later. Other than that, there is no difference :)

Regards,

##### Share on other sites
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 on other sites
int i=0;(1, i) = 1;

Is this valid?

##### 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).

(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 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 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 on other sites
By the way, I am not going to "do it". I am just writting a small CPP like compiler.

##### 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 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 on other sites
Quote:
 Original post by M2tMDo brackets enable this to be safe?i = (i++);

Nope. You need to add a sequence point. Brackets are used to make priority explicit, but in this case the priority would have been the same implicitly anyway. In this case, the value of i++ is evaluated before i = * anyway, but the side-effect of i++ need not be performed to get the value of i++ (in the case of ++i, this would have worked).

Quote:
 *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.

That's pretty simple: if two expressions are separated by a sequence point, the expression before it is entirely executed (along with its side-effects) before the expression after.

##### Share on other sites
Thanks for the response. It's learning about little things like this each day which humble you and make you realize how much you have yet to learn. The devil's in the details. Your time is appreciated.

*edit: Shoots, I'm sorry ToohrVyk. I tried re-rating you because I thought I had rated you a long time ago when I was at like 1100 hoping it would bump your rating one or two points but it dropped one :(

##### Share on other sites
to go back to this:

i = 1;
i = i++;

it executes like:
i = i;
i++; //i = 2

if you want to do it the other way, use:

i = ++i;

this does:
i++;
i = i; //i is also 2 after that

i = (i++);
works just fine, like the second example.
you can do this.
it does (when using a temp value):

temp = i++;
i = temp;

##### Share on other sites
Quote:
 Original post by Anonymous Posteri = (i++); works just fine, like the second example.

This does two things:
A- set i to its initial value

Since there is only one sequence point, the order of execution may be either AB or BA.

BA does not change i.

Therefore, it does not "work just fine". Except if by "works just fine" you meant "causes undefined behaviour".

##### Share on other sites
Quote:
 Original post by The C modest godI am just writting a small CPP like compiler.

Yikes, why?

##### Share on other sites
Quote:
 Original post by ToohrVyk(in the case of ++i, this would have worked).

It almost certainly would have worked, but the behavior is still undefined.

CM

##### Share on other sites
Quote:
 Original post by The C modest godWhat is the difference between:[...]

None, in both cases you hate yourself and your future readers. There might be justifiable uses of the comma operator, but I cannot think of one.

##### Share on other sites
Quote:
 Original post by M2tM*edit: Shoots, I'm sorry ToohrVyk. I tried re-rating you because I thought I had rated you a long time ago when I was at like 1100 hoping it would bump your rating one or two points but it dropped one :(

I fixed it. [wink] Not sure why I hadn't rated him yet though.

##### Share on other sites
Quote:
Original post by DaBookshah
Quote:
 Original post by The C modest godI am just writting a small CPP like compiler.

Yikes, why?

I want to remove some of the game CPP code into my script code.
Also, I want a future possiblity for players to add content to my games.
And maybe also a possiblity for programs to program themself.

##### Share on other sites
Quote:
Original post by tstrimp
Quote:
 Original post by M2tM*edit: Shoots, I'm sorry ToohrVyk. I tried re-rating you because I thought I had rated you a long time ago when I was at like 1100 hoping it would bump your rating one or two points but it dropped one :(

I fixed it. [wink] Not sure why I hadn't rated him yet though.

I suppose you didnt think to rate me up? Geez.
I am just joking. I am the... god dammit, how did that boxer called him self when he was sent to jail?
Anyway, I have simply stopped wanting to be rated up instead of trying to get rated up.

##### Share on other sites
Quote:
 Original post by The C modest godI want to remove some of the game CPP code into my script code.Also, I want a future possiblity for players to add content to my games.And maybe also a possiblity for programs to program themself.

Then why no impliment one of the hundreds of scripting languages already out there?

Quote:
 Original post by The C modest godI suppose you didnt think to rate me up? Geez.

Point me to a post where you have been helpful [wink]

##### Share on other sites
Quote:
Original post by tstrimp
Quote:
 Original post by The C modest godI want to remove some of the game CPP code into my script code.Also, I want a future possiblity for players to add content to my games.And maybe also a possiblity for programs to program themself.

Then why no impliment one of the hundreds of scripting languages already out there?

Quote:
 Original post by The C modest godI suppose you didnt think to rate me up? Geez.

Point me to a post where you have been helpful [wink]

Because I like CPP.

##### Share on other sites
That's a terrible reason.

Many of the available scripting languages out there have similar enough syntax to C++. You're not going to be able to implement a fully-syntax-compliant (let alone actually standards compliant) C++ interpreter in any reasonable amount of time.

What you are doing is an excersize in futility.

But, that said, I'm sure you won't be changing your mind. So I would recommend you invest in a copy of the C++ standard -- you actually probably don't need to even buy one, the drafts to the older standards available online. For example, here's the 1996 draft.

##### Share on other sites
Quote:
 Original post by jpetrieThat's a terrible reason.Many of the available scripting languages out there have similar enough syntax to C++. You're not going to be able to implement a fully-syntax-compliant (let alone actually standards compliant) C++ interpreter in any reasonable amount of time.What you are doing is an excersize in futility.But, that said, I'm sure you won't be changing your mind. So I would recommend you invest in a copy of the C++ standard -- you actually probably don't need to even buy one, the drafts to the older standards available online. For example, here's the 1996 draft.It will help you answer some of the more esoteric questions you'll need. It may not be totally up-to-date, but you don't really need the latest version for your purposes anyway.

What other script/languages do you suggest?
By the way, I am not only interperting the language, I mainly work on a compiler.
Which means I compile the code into some CPP classes which I run with my program.
I also plan to give some interperting capabilities, but the main functionality would be compilation.

I am also probabbly not going to support all the features in CPP, such as templates.
I also have all the variables in this script as referneces. There is no by value.
So int will act like the int in CPP but it will be implemented by reference.
So when passing an int to a function it will change the value of the variable from the scope that called the function.

##### Share on other sites
Quote:
 What other script/languages do you suggest?

Lua, Squirell, Python, AngelCode... There's a bunch. "Ch" is a C interpreter you might want to look at.

Quote:
 By the way, I am not only interperting the language, I mainly work on a compiler.Which means I compile the code into some CPP classes which I run with my program.

Um. What benefit do you think that gives you?

One of the reasons to use a scripting language is to allow for modification of system without recompilation of your application. But if all you're doing is generating C++ code that needs to be compiled... you're really wasting your time! Especially since you're generating C++ code from, essentially, C++ code. You're just applying a transformation to the text, which is better off done as a prebuild step using a program written in a language with strong text-manipulation abilities (like Python or Perl).

"Compiling" your "C++-like" scripting language into C++ code allows for exactly none of the benefits of a scripting language (you can't iterate quickly, and users can't modify the scripts because they can't recompile your program). So you're going to put a lot of effort into a non-trivial task for zero benefit -- you might as well be writing the "generated" C++ output directly, it will be faster.

If you really want scripting capability, look into using a real scripting language. If you just want to transform C++, write an external tool in Python or Perl to do it, and run that tool as a pre-build step in your IDE.