It's also occasionally useful in the construction of #define macros, where readability is often sacrificed for proper semantics regarding side effects and part-of-speech.
C++ allows overloading the comma operator. This can be (ab)used to make the following syntax work for, e.g., initializing a vectorish type:
MyType obj = 1, 2, 3;
Due to the precedence of operators, this is actually equivalent to
((MyType obj = 1), 2), 3;
so as long as the overloaded assignment operator returns an object that the comma operator mutates and returns, this can be made the have effect of initializing the declared object to a "list".
I think that when you overload the comma operator in C++, you change the nature of the sequence point. I believe you're no longer guaranteed to have "1, 2, 3" evaluated in that order. In your example, this doesn't matter, but if it was "f1(), f2(), f3()" and for some reason the sequence of those calls determined what each returned, you might have a problem. I could be wrong here though--my C++ is rusty from years of disuse.
Item #7 in More Effective C++ is to "never overload &&, || or ," because it changes the order of evaluation (with the boolean logic operators, it changes because the usual short-circuiting behavior is gone since the second argument has to be evaluated to pass it as an argument).
So is class string, so we're really talking about uses for the operator in C/C++.
I'd say the for version is better if you can declare the variable in the initialization section (tighter scope), but the while version has you repeat yourself less if the variable's already there.
This is what I was going to post, comma operator is really useful in macro for example when overriding mutex lock/unlock to call printf before and still keep the returned value and not change all calls.
I find the comma useful in deletion scenarios. For example, rather than just "free(x)" or "delete x" (after which the variable is undefined and can't be used anyway), I like to use the comma to add a NULL assignment. So, "free(x), x = NULL". It doesn't really need to be that way, but it's nice for avoiding random behavior in case the variable is referenced later; and the chain on one line makes it unlikely that the 2nd part will be forgotten.
For those who want to see a serious use of this operator, see http://code.google.com/p/cii/source/browse/trunk/include/exc... line 35. This from an exception (try, catch, except macros) library for the C language. It is from a book called "C interfaces and implementations", probably one of the best books on good C programming I have ever (and still) read.
It's special in the fact that it's written by Hanson (who also co-wrote the LCC compiler, you might have seen ID Software use this compiler) and the book uses Knuth's Literary Programming technique for the source code in the book ;)
26 comments
[ 3.1 ms ] story [ 65.6 ms ] threadThis trick is used by Boost Spirit to allow things like (example from http://stackoverflow.com/questions/54142/c-comma-operator)
http://stackoverflow.com/questions/52550/what-does-the-opera...
versusI'd say the for version is better if you can declare the variable in the initialization section (tighter scope), but the while version has you repeat yourself less if the variable's already there.
while (strlen(read_string(s)) > 5)
It's special in the fact that it's written by Hanson (who also co-wrote the LCC compiler, you might have seen ID Software use this compiler) and the book uses Knuth's Literary Programming technique for the source code in the book ;)