开发者

Can someone explain this C/C++ syntax?

开发者 https://www.devze.com 2023-02-03 04:37 出处:网络
Can sombody explain how this works? int x, y; .... (some_condition ? x : y) = 100; Is this intended to work or is is just a \"blind\" translation or the compiler (something like vec[10] equals 10[v

Can sombody explain how this works?

int x, y;
....
(some_condition ? x : y) = 100;

Is this intended to work or is is just a "blind" translation or the compiler (something like vec[10] equals 10[vec开发者_开发问答])?


This is valid C++ and invalid C.

The result of a conditional expression can be (and in this case is) an lvalue in C++ refering to one of x or y depending on whether some_condition evaluates to true. In C++ either x is assigned the value 100 if some_condition is true when converted to a bool, otherwise y is assigned 100.

In C, the result of a conditional expression is never an lvalue and cannot be assigned to.


At least in C++, that code snippet is essentially equivalent to this:

if(some_condition)
{
    x = 100;
}
else
{
    y = 100;
}

It is guaranteed by the C++ standard, although you have to read the relevant sections carefully. The rules of the operator are surprisingly complicated (mainly due to the type conversions that are performed) so the conditional operator and the if-then-else statement are not exactly equivalent all the time.

However, in your code snippet above, this paragraph in the standard is relevant:

5.16/4 Conditional operator:

If the second and third operands are lvalues and have the same type, the result is of that type and is an lvalue.


It is an expression which has the same result as this code:

if (some_condition)
  x = 100;
else
  y = 100;


if x and y are l-value, the ternary expression is an l-value.

Discussed here


No, it is a short form for a if condition. It is like

if(somecondition)
{
  x = 100;
}
else
{
  y = 100;
}


x and y are both lvalue of the same type. I do not think that there is something blind in this code. But you may find some compiler that cannot compile this code.

I usually prefer use if/else code which produce exactly the same code and is far more readable for maintainers. And if you want to track bug it easier to put each branch on separate branch it is easier to set break points.

Code coverage control is also easier to check.


you assign to either x or y depending on the condition.


It looks like a ternary operator selecting an lvalue. I didn't know that could be done, but I suppose it rather makes sense. Based on the condition, one of the two values (x or y) will be assigned the number 100.

Very cool if it works!


It is intended to work.

The ternary operator ?: can produce an lvalue (i.e. something you can assign to) if both of its possible results are lvalues, as they are in your example.

So your example assigns a value to either x or y depending on some other value.


The value of x or y being replaced with 100 depends on the condition. It is also generally used while returning -

return condition ? x : y ; // If condition is true => return x else return y 
0

精彩评论

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