What does ?: mean in C++? [duplicate] - c++

This question already has answers here:
?: ternary conditional operator behaviour when leaving one expression empty
(2 answers)
Closed 2 years ago.
I found this piece of code:
std::string(avdInfo_getSystemImagePath(m_avd)
?: avdInfo_getSystemInitImagePath(m_avd))
I only found information about the conditional operator: http://www.cplusplus.com/articles/1AUq5Di1/
That is, ? and : are separated. But what does it mean when they are together? both avdInfo_getSystemImagePath and avdInfo_getSystemInitImagePath return char*

It's a GCC extension.
x ?: y
is the same as
x ? x : y
Except that x will only be evaluated once.

Related

What is the scope and evaluation of this if [duplicate]

This question already has answers here:
Declaring and initializing a variable in a Conditional or Control statement in C++
(9 answers)
Defining a variable in the condition part of an if-statement?
(5 answers)
Closed 1 year ago.
When cleaning up some code that I have found online, I came across with this weird c++ line:
if (int i = 1) std::cout << i;
With LLVM it compiled fine and the console output is 1, but how does the scope is handled in here, shouldn't the i variable be only accessible inside the conditional (inside the parenthesis)? And how is that possible to be evaluated to true, isn't an assignment a void operation and with no value, so 0/false? What is going on with this line?

Is this the proper way to use the bool 'or' operator? [duplicate]

This question already has answers here:
Can you use 2 or more OR conditions in an if statement? [duplicate]
(9 answers)
Most efficient way to compare a variable to multiple values?
(7 answers)
Closed 2 years ago.
userString[count] == '!' || '.' || '?'
I am trying to say if the left equals ! or .(period) or ?

What do square brackets mean in this C++ statement? [duplicate]

This question already has answers here:
What is a lambda expression in C++11?
(10 answers)
Strange bracket-parentheses notation in C++, looking somewhat like a for each loop
(3 answers)
What does "[ this ]" mean in C++
(1 answer)
Closed 2 years ago.
I'm going through Wt web framework tutorial, in one example they initialize a variable this way:
auto greet = [this]{
greeting_->setText("Hello there, " + nameEdit_->text());
};
What does square brackets mean in this type of initialization?

Why when using ternary operator with std::cout, the conidtion gets printed instead of the result? [duplicate]

This question already has answers here:
C++, ternary operator and cout
(2 answers)
Conditional operator used in cout statement
(3 answers)
Closed 2 years ago.
When I write
std::cout << 5 ? 'a' : 'b';
the output is 5? Why should I put the whole expression in parentheses? isn't the ternary operator as a whole an expression and should be evaluated first then output the result? And if std::cout consider that I wanna print the condition, what happens to the part after the condition? Why is it ignored?

Unsure what this while statement is doing [duplicate]

This question already has answers here:
How does the Comma Operator work
(9 answers)
Expression "variable, variable = value;"
(4 answers)
Closed 9 years ago.
So, I'm doing homework. I've encountered something I haven't seen before and cannot find a decent explanation of what it does. Basically,
Object object;
...
while((value1, value2) = function(object)) {
object.foo(value1, value2);
}
The (value1, value2) in the while statement really throws me. Any ideas?
Its a comma operator.
The result of the comma operator is the last value (the others are evaluated and discarded).
while((value1, value2) = function(object)) {
object.foo(value1, value2);
}
If value1 is just a variable and not an expression then it is equivalent too:
while(value2 = function(object)) {
object.foo(value1, value2);
}
If value1 is an expression then it is evaluated each time around the loop. The result is discarded, but if the expression has side effects these will take effect.