views:

1955

answers:

9

What does the following code do in C/C++?

if ( blah(), 5) {
 //do something
}
+61  A: 

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.

itsmatt
Interestingly (at least to me), is that the ordering of operations evidently isn't ensured when you overload the comma operator. In general, I don't think I'd ever do that though.
itsmatt
That is interesting...
Michael Burr
Reminds me of a bug I wrote a couple of days ago : wanted to writeCAnObject a( d, f ); but wound up writing CAnObject a = ( d, f );Unfortunately there was a conversion from f's type to CAnObject. Awch.
QBziZ
+1  A: 

I would say that depends on blah().

Ben Hoffstein
blah() will be called and we don't know the side effects. So answering the question "what will this code do?" is not possible.
Ben Hoffstein
I agree with you, Ben. There isn't enough info to say for sure.
itsmatt
for example, exceptions. Or if you prefer c, goto.
tfinniga
+38  A: 

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)

jop
No, C++ has operator, to take into account here
David Pierre
Yeah - forgot that you can override the comma operator in C++. I guess that's the real point of this question!
jop
A: 

The following was written assuming it is C code, either in a C file or within a C block of a C++ file:

It is a pointless if. It will call blah(), however the result of blah() is not considered by if at all. The only thing being considered is 5, thus the if will always evaluate to true. IOW you could write this code as

blah();
// do something

without any if at all.

Mecki
Yeah, unfortunately someone could have blah() return an object whose class had overloaded the comma operator and then who knows exactly what will happen. The comma operator could compare something to the 5 and return false, skipping the code in the brackets completely.
itsmatt
If it is C++ at all (even a C++ project might have a plain C file among the list of files); there is a C++ tag, but also a C tag and this could be as well within a extern "C" block, in which case no overloading is possible.
Mecki
+13  A: 

In the pathological case, it depends on what the comma operator does...

class PlaceHolder
{
};

PlaceHolder Blah() { return PlaceHolder(); }

bool operator,(PlaceHolder, int) { return false; }

if (Blah(), 5)
{
    cout << "This will never run.";
}
Eclipse
+17  A: 

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.

Arkadiy
+1  A: 

On a more broad answer. The comma operator (non overloaded) resolves as in, execute the first part and return the second part.

So if you have (foo(),bar()) Both functions will be executed, but the value of the expression evaluates to bar() (and the type of the expression as well).

While I won't say there are fair usages for that, is usually considered a bit hard to read code. Mainly because not many languages shares such constructs. So As a personal rule of thumb I avoid it unless I am adding code to a preexistent expression and don't want to change completely its format.

Example: I have a Macro (not discussing if you should use macros or not, sometimes its not even you that wrote it)

FIND_SOMETHING(X) (x>2) ? find_fruits(x) : find_houses(x)

And I usually use it in assignments like my_possession = FIND_SOMETHING(34);

Now I want to add log to it for debuggin purposes but I cannot change the find functions,. I could do :

FIND_SOMETHING(X) (x>2)? (LOG("looking for fruits"),find_fruits(x)):(LOG("looking for houses"),find_houses(x))

OldMan
A: 

I use sometimes constructs like this for debugging purposes. When I force the if close to be true regardless of the return value of blah. It's obvious that it should never appear in production code.

A: 

Has anyone ever used the comma operator in a real situation? I tried to write some "clever" macros using it but could never get it to work the way I wanted to.

Sure, in the first part of a for(;;) statement.
MSalters