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

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

4
  • 25
    In this particular case of course, you can just replace it with return f==r; Commented Apr 27, 2009 at 21:15
  • 7
    @Eclipse: I wouldn't rely on an implicit conversion bool->int if I can avoid it. Commented Jun 23, 2009 at 7: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.) Commented Feb 5, 2015 at 4:08
  • 4
    @MichaelHoffmann The behaviour of the implicit conversion in this case is well defined, so using it is perfectly correct; see this answer 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. Commented Feb 5, 2015 at 9:10

8 Answers 8

171

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.

Sign up to request clarification or add additional context in comments.

4 Comments

In regular code, it's syntactic sugar, but it does enable you to do conditional initialization in the initialization list of the constructot.
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: Well, in C++11 you could consider a? b : c syntactic sugar for [&]() -> Type { if (a) return b; else return c; }().
Probably, but the question, answer, and comment were written in 2009.
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.

2 Comments

worthwhile to know that there is a sequence point at the '?'. That means the following is valid: ++x ? x : y;
@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.
14

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; 

1 Comment

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.
6

You can just rewrite it as:

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

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

1 Comment

this would perform implicit conversion from boolean to int
5

It is called the conditional operator.

You can replace it with:

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

Comments

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.

Comments

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.

Comments

2

It read as:

If f == r then return 1 else return 0 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.