107
int qempty()
{
    return (f == r ? 1 : 0);
}

In the above snippet, what does "?" mean? What can we replace it with?

Yun
  • 3,056
  • 6
  • 9
  • 28
Thaier Alkhateeb
  • 585
  • 5
  • 8
  • 18
  • 24
    In this particular case of course, you can just replace it with return f==r; – Eclipse Apr 27 '09 at 21:15
  • 7
    @Eclipse: I wouldn't rely on an implicit conversion bool->int if I can avoid it. – Daniel Daranas Jun 23 '09 at 07:10
  • 2
    @DanielDaranas why not? (This is kind of a beginner question- an explanation of your comments for beginners would be very helpful and appreciated.) – Michael Hoffmann Feb 05 '15 at 04:08
  • 4
    @MichaelHoffmann The behaviour of the implicit conversion in this case is well defined, so using it is perfectly correct; see [this answer](http://stackoverflow.com/a/5369783/96780) for a reference to the standard. Personally, I avoid using implicit type conversions because I think the code is more readable and maintainable and less error prone without them. I wrote in more detail about it in this [blog post](http://thedeepbluecpp.blogspot.com.es/2014/01/rule-2-make-all-type-conversions.html). – Daniel Daranas Feb 05 '15 at 09:10

8 Answers8

167

This is commonly referred to as the conditional operator, and when used like this:

condition ? result_if_true : result_if_false

... if the condition evaluates to true, the expression evaluates to result_if_true, otherwise it evaluates to result_if_false.

It is syntactic sugar, and in this case, it can be replaced with

int qempty()
{ 
  if(f == r)
  {
      return 1;
  } 
  else 
  {
      return 0;
  }
}

Note: Some people refer to ?: it as "the ternary operator", because it is the only ternary operator (i.e. operator that takes three arguments) in the language they are using.

Daniel LeCheminant
  • 50,583
  • 16
  • 120
  • 115
  • 5
    In regular code, it's syntactic sugar, but it does enable you to do conditional initialization in the initialization list of the constructot. – JohnMcG Apr 28 '09 at 03:11
  • Foo(Bar* y) pMember (y == NULL ? NULL : y->pMember) -- Here, we initialize pMember to y's pMember, or NULL if it's not there. Can't put if-else in a constructor initialization, so ternary operator makes it possible. – JohnMcG Sep 12 '10 at 12:38
  • @JohnMcG: Well, in C++11 you could consider `a? b : c` syntactic sugar for `[&]() -> Type { if (a) return b; else return c; }()`. – celtschk Sep 07 '14 at 10:09
  • 2
    Probably, but the question, answer, and comment were written in 2009. – JohnMcG Sep 12 '14 at 19:09
17

This is a ternary operator, it's basically an inline if statement

x ? y : z

works like

if(x) y else z

except, instead of statements you have expressions; so you can use it in the middle of a more complex statement.

It's useful for writing succinct code, but can be overused to create hard to maintain code.

Richard
  • 1,169
  • 6
  • 8
  • 4
    worthwhile to know that there is a sequence point at the '?'. That means the following is valid: ++x ? x : y; – Johannes Schaub - litb Apr 27 '09 at 21:32
  • 1
    @Daniel, that's what I meant by having expressions rather than statements. I probably wasn't explicit enough about the difference, so thanks for adding some clarification. – Richard Apr 27 '09 at 21:37
13

Just a note, if you ever see this:

a = x ? : y;

It's a GNU extension to the standard (see https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html#Conditionals).

It is the same as

a = x ? x : y;
  • in CLang (at least the most recent versions) this extension is also available. It's available even with C++11 flag turned off in a qmake project. So an expression like int `x = 1+1 ? : 0 `; correctly returns `2 `, in my compiler and this didn't complain anything. – Vinícius A. Jorge Dec 08 '15 at 17:38
6

You can just rewrite it as:

int qempty(){ return(f==r);}

Which does the same thing as said in the other answers.

5

It is called the conditional operator.

You can replace it with:

int qempty(){ 
    if (f == r) return 1;
    else return 0;
}
jjnguy
  • 136,852
  • 53
  • 295
  • 323
3

It's the conditional operator.

a ? b : c

It's a shortcut for IF/THEN/ELSE.

means: if a is true, return b, else return c. In this case, if f==r, return 1, else return 0.

Joe
  • 41,484
  • 20
  • 104
  • 125
2

It read as:

If f == r then return 1 else return 0
Henry Davis
  • 101
  • 1
  • 9
2

The question mark is the conditional operator. The code means that if f==r then 1 is returned, otherwise, return 0. The code could be rewritten as

int qempty()
{
  if(f==r)
    return 1;
  else
    return 0;
}

which is probably not the cleanest way to do it, but hopefully helps your understanding.

ssakl
  • 700
  • 2
  • 10
  • 23