   [C++] comma operator, when to use
  hi, I do know that the comma operator can be use to act as a sequence point, but when exactly is it a good time to use it? I have seen usage of it in some templates, like (a+=b, a+=b/2), but generally when do one use a comma operator? Since most statement can be separated. The only time where perhaps it is useful, is maybe in a for-loop condition?   
  Yeah it's pretty rarely used. Most often you can use a semi-colon instead, and doing so would make the code easier for other people to read.For loops is really the the only place I use it, but even then it's pretty rare to write loops like this:for( int i=0, j=0; i < x && j < y; ++i, j+=3 )Sometimes I use it in macro type code where I'd rather things be dense than readable... *guilty look*   ;  It's pretty much never useful. Like the ternary operator it can almost be replaced by more common code constructs, except in conditions.Unlike the ternary operator it's also pretty useless in conditions, because the the first operand does nothing if it doesn't have side effects and basing conditions on side effects that happen during the condition is an excellent way to confuse people and should therefor be avoided.It can be useful if you want to / have to write something very concise, like a macro or a complex for statement, but those cases are rare.In my professional programming career (10+ years), I have only encountered a single case where I had to resort to the comma operator and that was to patch a macro that was pretty shifty in the first place. Using the comma operator in this case was way less work than refactoring the macro.Finally, you'd be surprised how many programmers have not heard / have forgotten about the comma operator and only come across it, when they type a comma instead of a semicolon (a fairly common typo on some keyboard layouts). Interestingly enough this doesn't in most cases cause any errors or ill behavior!   ;  And perhaps the for-loop increment step. It can come in handy in some kind of loops, that take some set-up before evaluating the condition:while (setup(), test_condition()) {    actions();where the alternatives are://reduplicationsetup()if (test_condition()) {    do {   action();   setup();    while (test_condition());}//test in the middle of the loopwhile (true){    setup();    if (!test_condition()) break;    action();} The operator can also be overloaded (e.g see boost.assign), but overloading comma is generally a rather controversial issue.----The ternary operator might have some irreplaceable functions: try initializing something to one value or a another with an if-else block...   ;  What Hodgman said. Using it in for-loops is idiomatic. In other places it mostly reads like, "Oh, and this guy is using the comma operator for some reason."   ;Quote:Original post by visitorIt can come in handy in some kind of loops, that take some set-up before evaluating the condition:while (setup(), test_condition()) {    actions();You have a point with the above. However, I think in most real life cases, test_condition() wouldn't naturally need to be an independent function and could just be the return value of whatever the function setup() is doing.Quote:The operator can also be overloaded (e.g see boost.assign), but overloading comma is generally a rather controversial issue.I didn't know that it could be overloaded; that's just weird.;Quote:Original post by jwezorekI didn't know that it could be overloaded; that's just weird.Great for code obfuscation purposes ;)   ;  When you overload comma, then that is probably always (except intentional obfuscation) done for syntactic sugar. That's what makes it controversial. But then, the entire boost.assign library is there for syntactic sugar, because currently initializing containers with a set of known values is a PITA.   ;Quote:Original post by visitor... because currently initializing containers with a set of known values is a PITA.Yeah, intitialization is the most glaring place where the STL containers feel awkward to use, but I guess C++0x is taking care of this issue.;  wow thats fast replies. haha so i guess i shouldn't be feeling weird why I seldom ever need to use the comma operator haha. =)For the code we can write it as following isn't it? or am i wrong?Setup();while(TestCondition()){   Action();   Setup();}