开发者

Conditional operator always replaceable by if/else?

Until now I was thinking the conditional operator int a = b == 2 ? x1 : x2; is always replaceable by an if/else statement.开发者_Go百科

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

And the choice between one of the two is always a matter of taste. Today I was working with a task where a reference would be useful if I could write:

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

This is not allowed and I tried the initialization of the reference with the conditional operator. This was working and I came to realize, that the conditional operator is not always replaceable by an if/else statement.

Am I right with this conclusion?


You are correct. The conditional operator is an expression, whereas if-else is a statement. An expression can be used where a statement can be used, but the opposite is not true.

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

When C++ gets lambda expressions, then you may be able to use a lambda with an if-else in place of a conditional.


Well, there are obviously lots of places that you can't place an if. For example:

func( x ? 0 : 1 );

There is no way of writing that with an if statement. And this is a dupe, several times, not that I blame you for not finding it, because I can't.


Not exactly. You can always replace the reference (not re-seatable) with a pointer (re-seatable). So it's a matter of context.

E.g. you can write

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

No problemo, as someone once remarked to the Terminator.

And going all out for maximum "ugh what's that" effect,

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

But it's more clear with the conditional operator in this case. :-)

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

Cheers & hth.,


You are going to have more problems than that

// works
ostream *o;
if(x)
  o = &myfiles;
else
  o = &mystrings;


// stringstream* and fstream* -- incompatible!
ostream *o = x ? &myfiles : &mystrings;


You are right.

In C++ there are conditional assignment situations where use of the if-else statement is impossible, since this language explicitly distinguishes between initialization and assignment.

Furthermore, the ternary operator can yield an lvalue, i.e. a value to which another value can be assigned.

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

See also ?: ternary operator in C++.


You can't use it directly, but you can always get around that restriction by turning your conditional into something that is evaluated as 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 may be overkill for ints.


It depends on your definition of replaceable. For example, within a single function call, I cannot replace the following conditional operator with an if-else.

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

However, with the addition of another function, I've effectively replaced the conditional operator with an if-else.

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

int main()
{
  const int& i = get_i(x);
}


Acording to kodify.net we cannot replace every if/else statement with the conditional operator. There are two requirements for doing so: there must be one expression in both the if and the else block.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜