Disallow using comma operator

Posted by RiaD on Stack Overflow See other posts from Stack Overflow or by RiaD
Published on 2011-11-15T16:49:17Z Indexed on 2011/11/16 9:52 UTC
Read the original article Hit count: 290

Filed under:

I never use the comma operator.
But sometimes, when I write some recursions, I make a stupid mistake: I forget the function name. That's why the last operand is returned, not the result of a recursion call.

Simplified example:

int binpow(int a,int b){
    if(!b)
        return 1;
    if(b&1)
        return a*binpow(a,b-1);
    return (a*a,b/2); // comma operator
}

Is it possible get a compilation error instead of incorrect, hard to debug code?

© Stack Overflow or respective owner

Related posts about c++