Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将逗号运算符与条件运算符一起使用 我学习C++,在使用、和:< /Cord>运算符时,遇到以下行为。条件运算符的语法如下E1?E2:E3,其中E1、E2和E3是表达式[1]、[2]。我从以下代码开始: #include <iostream> using namespace std; int main(){ int x = 20, y = 25; x > y ? cout << "x > y\n" , cout << "x is greater than y" : cout << "x !> y\n", cout << "x is not greater than y"; return 0; }_C++_Conditional Statements_Operator Keyword_Comma - Fatal编程技术网

将逗号运算符与条件运算符一起使用 我学习C++,在使用、和:< /Cord>运算符时,遇到以下行为。条件运算符的语法如下E1?E2:E3,其中E1、E2和E3是表达式[1]、[2]。我从以下代码开始: #include <iostream> using namespace std; int main(){ int x = 20, y = 25; x > y ? cout << "x > y\n" , cout << "x is greater than y" : cout << "x !> y\n", cout << "x is not greater than y"; return 0; }

将逗号运算符与条件运算符一起使用 我学习C++,在使用、和:< /Cord>运算符时,遇到以下行为。条件运算符的语法如下E1?E2:E3,其中E1、E2和E3是表达式[1]、[2]。我从以下代码开始: #include <iostream> using namespace std; int main(){ int x = 20, y = 25; x > y ? cout << "x > y\n" , cout << "x is greater than y" : cout << "x !> y\n", cout << "x is not greater than y"; return 0; },c++,conditional-statements,operator-keyword,comma,C++,Conditional Statements,Operator Keyword,Comma,这是我期待的结果。但当我将值更改为intx=25,y=20,使x大于y时,我得到以下输出: x > y x is greater than y x is not greater than y 但我期待着: x > y x is greater than y 因此,即使表达式E1的结果为true,也会计算表达式E3的最后一部分 然而,当我把E2和E3放在括号内时,程序的输出与预期的一样:当x>y和当x为了简单起见,让我们考虑一个更容易阅读的语句: foo?a(),b():c(),d

这是我期待的结果。但当我将值更改为
intx=25,y=20,使x大于y时,我得到以下输出:

x > y
x is greater than y
x is not greater than y
但我期待着:

x > y
x is greater than y
因此,即使表达式
E1
的结果为
true
,也会计算表达式
E3
的最后一部分

然而,当我把E2和E3放在括号内时,程序的输出与预期的一样:当x>y和当x
与操作数E1和E2位于运算符上,如[1]中的
E1,E2
,这是一个表达式本身。基于此,我不理解为什么即使表达式
E1
为真且两者都为真,也要计算
?:
运算符表达式E3的最后一部分

我的问题是:

1) 我是否正确使用了条件运算符
?:

2) 发生这种意外结果的机制是什么

3) 为什么使用括号解决了这个问题(或者至少符合我的期望)

我正在使用:gcc版本5.4.0 20160609(Ubuntu 5.4.0-6ubuntu1~16.04.11)

多谢各位

[1]


(2)

< P>为了简单起见,让我们考虑一个更容易阅读的语句:

foo?a(),b():c(),d();
我们遇到的第一个运算符是条件运算符():

第二个操作数可以是表达式,
a(),b()
是复合表达式。 但是,第三个操作数只能是赋值表达式():

不是其中之一,而是逗号表达式(§):

由逗号分隔的一对表达式从左到右求值;左侧表达式是一个丢弃的值表达式。与左表达式关联的每个值计算和副作用都在与右表达式关联的每个值计算和副作用之前排序。[……]


在整个语句中,最后一个逗号运算符左侧的所有内容都是一个丢弃的值表达式,它在逗号运算符的右侧计算(及其结果被丢弃)。

为了简单起见,让我们考虑一个更容易阅读的语句:

foo?a(),b():c(),d();
我们遇到的第一个运算符是条件运算符():

第二个操作数可以是表达式,
a(),b()
是复合表达式。 但是,第三个操作数只能是赋值表达式():

不是其中之一,而是逗号表达式(§):

由逗号分隔的一对表达式从左到右求值;左侧表达式是一个丢弃的值表达式。与左表达式关联的每个值计算和副作用都在与右表达式关联的每个值计算和副作用之前排序。[……]


因此,整个语句中最后一个逗号运算符左侧的所有内容都是一个被丢弃的值表达式,它在逗号运算符右侧之前被计算(其结果被丢弃)。

您可以不使用逗号运算符而输出两件事:
cout With operator priority(或语法规则):
cond?E1,E2:E3,E4相当于
(cond?(E1,E2):E3),E4cout With operator priority(或语法规则):
cond?E1,E2:E3,E4相当于
(cond?(E1,E2):E3),E4d()
实际上并不是被“视”为
条件运算符的操作数:
而只是被视为程序员(我自己)碰巧使用逗号运算符编写的另一个表达式?换句话说,它被解释为在下面的代码块中<代码>x>y?换句话说,它被解释为在下面的代码块中是的,这是否意味着表达式
d()
实际上不是作为
?:
条件运算符的操作数“看到的”,而是程序员(我自己)碰巧使用逗号运算符编写的另一个表达式?换句话说,它被解释为在下面的代码块中<代码>x>y?换句话说,它被解释为在下面的代码块中对
x > y
x is greater than y
conditional-expression:
    logical-or-expression
    logical-or-expression ? expression : assignment-expression
assignment-expression:
    conditional-expression
    yield-expression
    throw-expression
    logical-or-expression assignment-operator initializer-clause
assignment-operator: one of
    =  *=  /=  %=   +=  -=  >>=  <<=  &=  ^=  |=
expression:
    assignment-expression
    expression , assignment-expression