In the past, when C++ compilers had less advanced optimizers, the ternary operator condition ? expr1 : expr2 was useful because it often produced more efficient code than using a full if-else statement. This efficiency came from two key reasons.
➜ the ternary operator is an expression, not a statement, so the compiler could better inline the code and avoid jumps or extra control flow logic. This led to fewer instructions and better use of CPU pipelines, especially on simpler architectures.
➜ since the ternary operator directly returns a value, it allowed developers to write concise code that assigned or returned a value in a single expression, avoiding duplicated code or separate variable declarations that compilers at the time struggled to optimize away.
So the ternary operator gave programmers finer control over performance-critical code in ways the compilers couldn't yet handle automatically.
Today, modern C++ compilers have highly advanced optimizers that can analyze control flow, remove dead code, inline functions, and perform branch prediction-aware transformations. Because of this, the performance difference between a ternary operator and an equivalent if-else statement has largely disappeared in most cases.
Compilers can now recognize simple conditional patterns and generate the same efficient machine code regardless of whether a ternary or an if-else is used, as long as the semantics are the same. They can also eliminate redundant assignments, fold constants, and even move computations out of branches if safe.
==> the choice between a ternary operator and if-else is now mainly about readability and style, not performance.
PS/ As OP explained, the ternary operator is indispensable or super useful in contexts that require a single expression, such as initializing const, constexpr, references, or members, as well as in return statements / compact lambda bodies, default arguments, and macros, where if-else statements are syntactically invalid.