40

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

if (blah(), 5) {
    //do something
}
3
  • Should specify the language in the question; I don't think tagging would be sufficient even if this didn't have both C and C++ tags.
    – Mark Baker
    Sep 30, 2008 at 14:27
  • @jfm3 not homework for me but someone in an IRC channel asked the question and i thought it was a good one @Mark thanks, i noted the language in the question
    – Matt
    Sep 30, 2008 at 14:31
  • "C/C++" is not a language. Most responders seem to focus on comma operator overloading, thus C++. The question should perhaps be re-tagged.
    – unwind
    Mar 11, 2009 at 20:45

8 Answers 8

66

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.

2
  • 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
    Sep 29, 2008 at 16:45
  • Reminds me of a bug I wrote a couple of days ago : wanted to write CAnObject a( d, f ); but wound up writing CAnObject a = ( d, f ); Unfortunately there was a conversion from f's type to CAnObject. Awch.
    – QBziZ
    Sep 29, 2008 at 18:11
45

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)

2
  • No, C++ has operator, to take into account here Sep 29, 2008 at 16:38
  • Yeah - forgot that you can override the comma operator in C++. I guess that's the real point of this question!
    – jop
    Sep 29, 2008 at 16:45
20

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.

13

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.";
}
1

I would say that depends on blah().

3
  • blah() will be called and we don't know the side effects. So answering the question "what will this code do?" is not possible. Sep 29, 2008 at 16:54
  • I agree with you, Ben. There isn't enough info to say for sure.
    – itsmatt
    Sep 29, 2008 at 17:15
  • for example, exceptions. Or if you prefer c, goto.
    – tfinniga
    Sep 29, 2008 at 18:43
1

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))

0

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.

0

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.

2
  • 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
    Sep 29, 2008 at 17:18
  • 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
    Sep 30, 2008 at 11:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.