What does the comma operator , do?

Comma operator is applied and the value 5 is used to determine the conditional's true/false.

It will execute blah() and get something back (presumably), then the comma operator is employed and 5 will be the only thing that is used to determine the true/false value for the expression.


Note that the , operator could be overloaded for the return type of the blah() function (which wasn't specified), making the result non-obvious.


I know one thing that this kind of code should do: it should get the coder fired. I would be quite a bit afraid to work next to someone who writes like this.


If the comma operator is not overloaded, the code is similar to this:

blah();
if (5) {
  // do something
}

If the comma operator is overloaded, the result will be based on that function.

#include <iostream>
#include <string>

using namespace std;

string blah()
{
    return "blah";
}

bool operator,(const string& key, const int& val) {
    return false;
}

int main (int argc, char * const argv[]) {

    if (blah(), 5) {
        cout << "if block";
    } else {
        cout << "else block";
    }

    return 0;
}

(edited to show comma operator overloading scenario. thanks to David Pierre for commenting on this)