Thursday, June 26, 2008

c++: coma operator

I use coma operator all the time:

int a, b;
for (a=1,b=1;a<3;++a,++b)
    ...
Probably some other cases. By specification coma operator allows to call a sequence of instructions where one is allowed. So
true?(++a,b+=2,a+b):(a+=2,++b,a-b)
will evaluate ++a,b+=2 and return a+b. The other awesome thing with coma that it breaks rule for necessity of braces for blocks of code. Multiply instruction can be folded into one:
if (a == b)
    a=1, cout << a+b;

2 comments:

Cheba said...

Last example illustrates strong WTF abilities of C++ language.

Ni@m said...

Every language has it's own WTF code style =). Perl especially ;)