左撇子使用有条件的 [英] left handed use of conditional

查看:80
本文介绍了左撇子使用有条件的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在想,当我编码

时我理解了条件运算符以下期望它失败:


int a = 10,b = 20,c = 0;

((a
a = 20; b = 10;

((a< b)?a:b)= c; // b = 0

现在,由于表达式5 = c无效,我没想到上面的

工作。但它确实有效。为什么?我认为?:运算符会将

评估为a的值。或b或b。不要某种指向价值的指针。这个

让我想知道操作员是如何工作的。

I was just thinking that I understood the conditional operator when I coded
the following expecting it to fail:

int a= 10, b= 20, c= 0;
((a < b) ? a : b) = c; // a=0
a=20; b= 10;
((a < b) ? a : b) = c; //b=0

Now since the expresion 5 = c is not valid I did not expect the above to
work. But it does work. Why? I thought that the ?: operator would evaluate
to the value of "a" or "b" not to some kind of pointer to the value. This
really leaves me wondering how the operator works.

推荐答案

>我只是觉得我理解了条件运算符,当我编码
>I was just thinking that I understood the conditional operator when I coded

>以下期望它失败:

int a = 10,b = 20,c = 0;
((a< b)?a:b)= c; // a = 0
a = 20; b = 10;
((a
现在,因为表达式5 = c无效,我没想到上面的工作。但它确实有效。为什么?我认为?:运算符会评估a的值。或b或b。不要某种指向价值的指针。这真的让我想知道操作员是如何工作的。
>the following expecting it to fail:

int a= 10, b= 20, c= 0;
((a < b) ? a : b) = c; // a=0
a=20; b= 10;
((a < b) ? a : b) = c; //b=0

Now since the expresion 5 = c is not valid I did not expect the above to
work. But it does work. Why? I thought that the ?: operator would evaluate
to the value of "a" or "b" not to some kind of pointer to the value. This
really leaves me wondering how the operator works.



我不是语言专家,用法确实很奇怪,但即使是

Comeau编译器接受它,所以我' 我猜它是完全有效的,并且确实如此:

如果(a
a = c;

其他

b = c;


Dave

I''m no language expert, and the usage does seem odd, but even the
Comeau compiler accepts it, so I''d guess it''s perfectly valid and does
something like this:

if ( a < b )
a = c;
else
b = c;

Dave


Nicholas M. Makin写道:
Nicholas M. Makin wrote:

我只是在想我理解条件运算符时我将
编码为以下内容,期望它失败:


int a = 10,b = 20,c = 0;

((a< b)?a:b)= c; // a = 0

a = 20; b = 10;

((a< b)?a:b)= c; // b = 0

现在,由于表达式5 = c无效,我没想到上面的

可以工作。但它确实有效。为什么?我认为?:运算符将

评估为a的值。或b或b。不要某种指向

的价值。这让我想知道操作员是如何工作的。
I was just thinking that I understood the conditional operator when I
coded the following expecting it to fail:

int a= 10, b= 20, c= 0;
((a < b) ? a : b) = c; // a=0
a=20; b= 10;
((a < b) ? a : b) = c; //b=0

Now since the expresion 5 = c is not valid I did not expect the above
to work. But it does work. Why? I thought that the ?: operator would
evaluate to the value of "a" or "b" not to some kind of pointer to
the value. This really leaves me wondering how the operator works.



这是由左值的概念产生的。和rvalues。 (基本上,

"左值'可以分配给,而'rvalue'不能分配。


条件运算符返回两个中的一个表达式。如果两个

表达式都是l值,那么条件的结果也是一个

l-value,正如你所发现的那样。如果任一表达式不是l值,则

那么l值的那个将受到左值转换为左值/b $ b转换的影响,并且表达式的结果将是一个r值而不是

可分配。


int a,b;

((a < b)?a:5)= 1; //不会编译


你会发现条件运算符也适用于其他可能令人惊讶的

的情况:


void f(int){...}

void g(int){...}

int a,b ;


((a< b)?f:g)(10); //用参数10调用f或g

int p [100],q [100];


((a< b)?p :q)[10] = 0; //将* p的第10个元素归零或* q


-cd


This results from the concepts of "lvalues" and "rvalues". (Basically, an
"lvalue" can be assigned-to, while an "rvalue" cannot).

The conditional operator returns one of two expressions. If both
expressions are l-values, then the result of the conditional is also an
l-value, as you''ve discovered. If either expression was not an l-value,
then the one that was an l-value would be subject to an "lvalue to rvalue
conversion", and the result of the expression would be an r-value and not
assignable.

int a, b;
((a < b ) ? a : 5) = 1; // won''t compile

You''ll find the conditional operator works in other possibly surprising
cases as well:

void f(int) { ... }
void g(int) { ... }

int a,b;

((a < b) ? f : g)(10); // calls either f or g with parameter 10
int p[100], q[100];

((a < b) ? p : q)[10] = 0; // zero out 10th element of *p or *q

-cd



对不起我不是故意向您发送电子邮件...
Sorry I did not mean to send you an email...

>条件运算符返回两个表达式之一。如果两个
表达式都是l值,那么条件的结果也是一个值,正如你所发现的那样。如果任一表达式不是l值,那么那个是l值的那个将受到左值到右值转换的影响,并且表达式的结果将是r -value而不是
可分配。
>The conditional operator returns one of two expressions. If both
expressions are l-values, then the result of the conditional is also an
l-value, as you''ve discovered. If either expression was not an l-value,
then the one that was an l-value would be subject to an "lvalue to rvalue
conversion", and the result of the expression would be an r-value and not
assignable.



现在这样做了。非常感谢。


至于其他例子。我确实找到了带有函数的示例a / b
有点令人惊讶但并不出人意料,因为标识符链接到函数的
referances。然而,我确实期望使用

指针的行为。事实上我的计划是用指针替换上面的代码

我完全可以预料到:


int * a,* b;


*((* a< * b)?a:b)= 0;


工作。我所知道的所有关于条件

运算符的东西告诉我它会的。


所有这一切都让我很困惑的是我正在使用条件运算符在

上编写教程。大多数此类教程只提供基本的

语法和标准示例。问题是他们从来没有真正指出条件运算符是一个*运算符*而不仅仅是一个if-else *语句的简写

*。所以我一直在寻找令人惊讶的例子,

将有助于推动这一点回家。 - 正如你所看到的那样,当我尝试的时候,我认为* b * b *会是一个反例,它就变成了一个例子。 :)


如果你知道一个很好的资源我可以找到更多的技术数据

或C / C ++ / C中条件运算符的惊人例子#我会

大大提高它。


再次感谢您清理rvalue-lvalue。我会做更多的研究

,以便我可以详细解释。

Now that makes sence. Thank you very much.

As for the other examples. I did find the example with the functions a
little surprising but not unexpected as the identifiers are linked to
referances to the functions. I did however expect the behavior with
pointers. In fact my plan had been to replace the above code with pointers
and I fully expected:

int *a, *b;

*((*a < *b) ? a : b) = 0;

To work. Everything I *thought* that I understood about the conditional
operator told me that it would.

What is kind of ammusing in all of this is that I was writing a tutorial on
using the conditional operator. Most such tutorials just give the basic
syntax and the standard example. The problem is that they never really point
out that the conditional operator is an *operator* and not just a shorthand
for an if-else *statement*. So I was looking for surprising examples that
would help drive this point home. -- And as you can see when I tried what I
*thought* would be a counter example it turned into an example. :)

If you know of a good resource where I could find some more technical data
or surprising examples on the conditional operator in C/C++/C# I would
greatly apreciate it.

Again thank you for clearing up the rvalue-lvalue. I will do more research
on that so that I can explain in detail.


这篇关于左撇子使用有条件的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆