The conditional operator is always replaced by if / else?

So far, I thought the conditional operator is int a = b == 2 ? x1 : x2; int a = b == 2 ? x1 : x2; always replaced by an if / else statement.

 int a; if (b == 2) a = x1; else a = x2; 

And the choice between one of them always depends on the taste. Today I worked with a task where the link would be useful if I could write:

 int& a; if (b == 2) a = x1; else a = x2; 

This is not allowed, and I tried to initialize the link with a conditional statement. This worked, and I realized that the conditional statement is not always replaced by the if / else statement.

Am I right with this conclusion?

+4
source share
7 answers

You're right. The conditional operator is an expression, while if-else is an expression. An expression can be used when an operator can be used, but the opposite is not true.

This is a good counterexample to show when you encounter someone who insists that you should never, never, never, never, ever use conditional expressions, because if-else is "simple" and conditional expressions " too complicated. "

When C ++ gets lambda expressions, then you can use lambda with if-else instead of conditional.

+11
source

Well, obviously, there are many places that you cannot accommodate if. For instance:

 func( x ? 0 : 1 ); 

It is not possible to write this using the if statement. And this is a hoax, several times, and not that I accuse you of not finding him, because I cannot.

+2
source

Not really. You can always replace the link (not replaceable on the seat) with a pointer (with the possibility of reuse). So this is a matter of context.

eg. You can write

 int* pa; if( b == 2 ) pa = &x1; else pa = &x2; int& a = *pa; 

No problem, as someone once noticed the Terminator.

And with all my might for the maximum effect of "ugh what that",

 int* pa; switch( b == 2 ) { case true: pa = &x1; break; default: pa = &x2; } int& a = *pa; 

But this is more clear with the conditional operator in this case. :-)

 int& a = (b == 2? x1 : x2); 

Cheers and hth.,

+2
source

You will have more problems than

 // works ostream *o; if(x) o = &myfiles; else o = &mystrings; // stringstream* and fstream* -- incompatible! ostream *o = x ? &myfiles : &mystrings; 
+2
source

You're right.

In C ++, there are situations of conditional assignment when using an if-else statement is not possible, since this language clearly distinguishes between initialization and assignment.

In addition, the ternary operator can give the value l, that is, a value to which another value can be assigned.

In addition, some compilers in some cases may generate different code for ternary vs conditional if-then. GCC, for example, performs better code optimization if the ternary operator is used.

See also ?: Ternary operator in C ++ .

+1
source

You cannot use it directly, but you can always get around this limitation by turning your conditional into something that evaluates to an expression ...

 int& initValue(int b, int& x1, int& x2){ if (b==2) return x1; return x2; } ... int& a = initValue(b, x1, x2); 

Of course, this can be excessive for ints.

+1
source

It depends on your definition of plug-in. For example, in one function call, I cannot replace the following conditional if-else .

 int n1 = 10; int n2 = 20; const int& i = x > 0 ? n1 : n2; 

However, with the addition of another function, I actually replaced the conditional if-else .

 const int& get_i(double x) { if(x > 0) return n1; else return n2; } int main() { const int& i = get_i(x); } 
0
source

Source: https://habr.com/ru/post/1332557/


All Articles