I’m browsing through some code and I found a few ternary operators in it. This code is a library that we use, and it’s supposed to be quite fast.
I’m thinking if we’re saving anything except for space there.
What’s your experience?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Performance
The ternary operator shouldn’t differ in performance from a well-written equivalent
if/elsestatement… they may well resolve to the same representation in the Abstract Syntax Tree, undergo the same optimisations etc..Things you can only do with ? :
If you’re initialising a constant or reference, or working out which value to use inside a member initialisation list, then
if/elsestatements can’t be used but?:can be:Factoring for concise code
Keys reasons to use
?:include localisation, and avoiding redundantly repeating other parts of the same statements/function-calls, for example:…is only preferable to…
…on readability grounds if dealing with very inexperienced programmers, or some of the terms are complicated enough that the
?:structure gets lost in the noise. In more complex cases like:An equivalent
if/else:That’s a lot of extra function calls that the compiler may or may not optimise away.
Further,
?allows you to select an object, then use a member thereof:The equivalent
if/elsewould be:Can’t named temporaries improve the if/else monstrosity above?
If the expressions
t1,f1,t2etc. are too verbose to type repeatedly, creating named temporaries may help, but then:To get performance matching
?:you may need to usestd::move, except when the same temporary is passed to two&¶meters in the function called: then you must avoid it. That’s more complex and error-prone.c
?x:y evaluates c then either but not both of x and y, which makes it safe to say test a pointer isn’tnullptrbefore using it, while providing some fallback value/behaviour. The code only gets the side effects of whichever of x and y is actually selected. With named temporaries, you may needif/elsearound or?:inside their initialisation to prevent unwanted code executing, or code executing more often than desired.Functional difference: unifying result type
Consider:
In the conditional operator version above,
1undergoes a Standard Conversion todoubleso that the type matched2.0, meaning theis(double)overload is called even for thetrue/1situation. Theif/elsestatement doesn’t trigger this conversion: thetrue/1branch callsis(int).You can’t use expressions with an overall type of
voidin a conditional operator either, whereas they’re valid in statements under anif/else.Emphasis: value-selection before/after action needing values
There’s a different emphasis:
An
if/elsestatement emphasises the branching first and what’s to be done is secondary, while a ternary operator emphasises what’s to be done over the selection of the values to do it with.In different situations, either may better reflect the programmer’s “natural” perspective on the code and make it easier to understand, verify and maintain. You may find yourself selecting one over the other based on the order in which you consider these factors when writing the code – if you’ve launched into “doing something” then find you might use one of a couple (or few) values to do it with,
?:is the least disruptive way to express that and continue your coding “flow”.