Conditional operator used in cout statement
The ?:
operator has lower precedence than the <<
operator i.e., the compiler interprets your last statement as:
(std::cout << (a != 0)) ? 42.0f : -42.0f;
Which will first stream the boolean value of (a!=0)
to cout. Then the result of that expression (i.e., a reference to cout) will be cast to an appropriate type for use in the ?: operator (namely void*
: see cplusplus.com), and depending on whether that value is true (i.e., whether cout has no error flags set), it will grab either the value 42 or the value -42. Finally, it will throw that value away (since nothing uses it).
C++, ternary operator, std::cout
Depends on what type is result1, result2
etc.
expressionC ? expression1 : expression2
isn't valid for all types of expression1
and expression2
. They must necessarily be convertible to a common type, roughly speaking (exact rules and exceptions can be read in the standard). Now, if result
s are strings, then you do it like this:
std::cout << ( condition1 ? result1 : "Error" )
^^^
<< ( condition2 ? result2 : "Error")
^^^
<< etc.
But if results are integers, for example, you can't do it.
HTH
Cout String and int in ternary operator
Ugly:
i%3==0 ? cout<< "Hello\n" : cout<<i;
Nice:
if ( i%3 == 0 )
cout << "Hello\n";
else
cout << i;
Your version doesn't work because the result types of the expressions on each side of :
need to be compatible.
C++, ternary operator and cout
cout << 5 ? (5 ? 0 : 2) : 5;
is parsed as
(cout << 5) ? (5 ? 0 : 2) : 5;
what is the output of conditional operator with unary operator
The expression on the right hand side is evaluated like an if-statement.
if (Delay == true)
return 1;
else
return -1;
The result is then used for the +=
assignment.
In the
C++20 draft standard that's
7.6.19 (6) (Assignment and compound assignment operators)
The behavior of an expression of the form
E1 op= E2
is equivalent toE1 = E1 op E2
except that E1 is
evaluated only once. [...]
Since Delay == false
, the return value of the ternary operator is -1
. The fact that you're operating on a bool
ean instead of an int
can make it look like you got the +1
back.
Note that you get a compiler warning C4804:
warning C4804: '+=': unsafe use of type 'bool' in operation
Is it undefined behavior? No.
7.6.19 (6) (Assignment and compound assignment operators)
[...] For += and
-=, E1 shall either have arithmetic type or be a pointer to a possibly cv-qualified completely-defined object
type. In all other cases, E1 shall have arithmetic type.
and
7.3.8 (2) (Integral conversions)
If the destination type is bool, see 7.3.14.
which says
7.3.14 (1) (Boolean conversions)
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer-to-member type can be converted to a
prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false;
any other value is converted to true.
So the -1 is converted to true
and true
prints as 1.
How do I use the conditional (ternary) operator?
It works like this:
(condition) ? true-clause : false-clause
It's most commonly used in assignment operations, although it has other uses as well. The ternary operator ?
is a way of shortening an if-else clause, and is also called an immediate-if statement in other languages (IIf(condition,true-clause,false-clause)
in VB, for example).
For example:
bool Three = SOME_VALUE;
int x = Three ? 3 : 0;
is the same as
bool Three = SOME_VALUE;
int x;
if (Three)
x = 3;
else
x = 0;
Related Topics
How to Export Templated Classes from a Dll Without Explicit Specification
Understanding the Example on Lvalue-To-Rvalue Conversion
What Is the Behavior of "Delete" with Stack Objects
Unoptimized Clang++ Code Generates Unneeded "Movl $0, -4(%Rbp)" in a Trivial Main()
Setting Vector Elements in Range-Based for Loop
Undefined Behaviour Somewhere in Boost::Spirit::Qi::Phrase_Parse
How to Get the Position and Draw Rectangle Using Opencv
At^Sysinfo and a C++ Terminal Program
What Is the Best Modern C++ Approach to Construct and Manipulate a 2D Array
Cycles in Family Tree Software
Why Is the C++ Stl Is So Heavily Based on Templates? (And Not on *Interfaces*)
How to Print the Full Value of a Long String in Gdb