What is the need for the conditional operator? Functionally it is redundant, since it implements an if-else construct. If the conditional operator is more efficient than the equivalent if-else assignment, why can't if-else be interpreted more efficiently by the compiler?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Sometimes the ternary operator is the best way to get the job done. In particular when you want the result of the ternary to be an l-value.
This is not a good example, but I'm drawing a blank on somethign better. One thing is certian, it is not often when you really need to use the ternary, although I still use it quite a bit.
One thing I would warn against though is stringing ternaries together. They become a real
problem at maintennance time:
EDIT: Here's a potentially better example. You can use the ternary operator to assign references & const values where you would otherwise need to write a function to handle it:
...could become:
Which is better is a debatable question that I will choose not to debate.
It's crucial for code obfuscation, like this:
Ternary operator may be of more performance than a normal if else clause, this may be critical in embedded applications but also compiler optimization may collapse this difference.
Some of the other answers given are great. But I am surprised that no one mentioned that it can be used to help enforce
const
correctness in a compact way.Something like this:
so basically
n
is aconst
whose initial value is dependent on a condition statement. The easiest alternative is to maken
not aconst
, this would allow an ordinaryif
to initialize it. But if you want it to beconst
, it cannot be done with an ordinaryif
. The best substitute you could make would be to use a helper function like this:but the ternary if version is far more compact and arguably more readable.
Since no one has mentioned this yet, about the only way to get smart
printf
statements is to use the ternary operator:Caveat: There are some differences in operator precedence when you move from C to C++ and may be surprised by the subtle bug(s) that arise thereof.
like dwn said, Performance was one of its benefits during the rise of complex processors, MSDN blog Non-classical processor behavior: How doing something can be faster than not doing it gives an example which clearly says the difference between ternary (conditional) operator and if/else statement.
give the following code:
the cost for different boundary are much different and wierd (see the original material). while if change:
to
The execution time is now independent of the boundary value, since:
but on my desktop intel i5 cpu/windows 10/vs2015, my test result is quite different with msdn blog.
when using debug mode, if/else cost:
and ternary operator cost:
when using release mode, if/else cost:
and ternary operator cost:
the ternary operator is slower than if/else statement on my machine!
so according to different compiler optimization techniques, ternal operator and if/else may behaves much different.