简体   繁体   English

将逗号运算符与条件运算符一起使用

[英]Using the comma operator with the conditional operator

I am learning C++ and stumbled upong the following behaviour when using the , and ?: operators.我正在学习 C++,并在使用,?:运算符时偶然发现了以下行为。 The conditional operator has a syntax like this E1 ? E2 : E3条件运算符的语法类似于E1 ? E2 : E3 E1 ? E2 : E3 , where E1, E2 and E3 are expressions[1],[2]. E1 ? E2 : E3 ,其中 E1、E2 和 E3 是表达式[1],[2]。 I started with this code:我从这个代码开始:

#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;
}

and outputs:和输出:

x !> y
x is not greater than y

which is the result I was expecting.这是我期待的结果。 But when I change the values to int x = 25, y = 20 so that x is greater than y, I get the following output:但是当我将值更改为int x = 25, y = 20以便 x 大于 y 时,我得到以下输出:

x > y
x is greater than y
x is not greater than y

but I was expecting:但我期待:

x > y
x is greater than y

so the final part of expression E3 is being computed even when the result of expression E1 was true .因此,即使表达式E1的结果为true也会计算表达式E3的最后部分。

However, when I put E2 and E3 inside parenthesis, the output of the programm is as expected for both cases: when x > y and when x < y.但是,当我将 E2 和 E3 放在括号内时,程序的输出在两种情况下都符合预期:当 x > y 和 x < y 时。 According to [1], the comma , is on operator with operands E1 and E2 as in E1, E2 , which is an expression itself, by [1].根据 [1],逗号,是操作数 E1 和 E2 的运算符,如在E1, E2 ,它是表达式本身,由 [1]。 Based on this, I do not understand why the final part of expression E3 for the ?: operator is being computed even when the expression E1 is true and both.基于此,我不明白为什么即使表达式E1为真且两者都为真,为什么还要计算?:运算符的表达式 E3 的最后部分。

My questions are:我的问题是:

1) Am I correctly using the conditional operator ?: ? 1)我是否正确使用条件运算符?:

2) What is the mechanism by which this unexpected result is happening? 2) 这种意想不到的结果发生的机制是什么?

3) Why does the use parenthesis solves the issue (or at least agrees with my expectation)? 3)为什么使用括号可以解决问题(或者至少符合我的期望)?

I am working with: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11)我正在使用:gcc 版本 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11)

Thank you very much.非常感谢。

[1] https://en.cppreference.com/w/cpp/language/expressions [1] https://en.cppreference.com/w/cpp/language/expressions

[2] https://en.cppreference.com/w/cpp/language/operator_other [2] https://en.cppreference.com/w/cpp/language/operator_other

For the sake of simplicity let's consider a statement that is easier to read:为了简单起见,让我们考虑一个更容易阅读的语句:

foo ? a(), b() : c(), d();

The first operator we encounter is the conditional operator ( §7.6.16 ):我们遇到的第一个运算符是条件运算符(第7.6.16 节):

conditional-expression:
    logical-or-expression
    logical-or-expression ? expression : assignment-expression

The second operand can be an expression and a(), b() is a compound expression.第二个操作数可以是一个表达式, a(), b()是一个复合表达式。 The third operand however can only be an assignment-expression ( §7.6.19 ):然而,第三个操作数只能是赋值表达式(第7.6.19 节):

assignment-expression:
    conditional-expression
    yield-expression
    throw-expression
    logical-or-expression assignment-operator initializer-clause
assignment-operator: one of
    =  *=  /=  %=   +=  -=  >>=  <<=  &=  ^=  |=
c(), d()

is not one of those but a comma-expression (§ 7.6.20/1 ):不是其中之一,而是逗号表达式(第7.6.20/1 节):

expression:
    assignment-expression
    expression , assignment-expression

A pair of expressions separated by a comma is evaluated left-to-right;一对用逗号分隔的表达式从左到右求值; the left expression is a discarded-value expression.左边的表达式是一个弃值表达式。 Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression.与左表达式关联的每个值计算和副作用在与右表达式关联的每个值计算和副作用之前排序。 [...] [...]

So everything on the left side of the last comma operator in the whole statement is a discarded-value expression that gets computed (and its result discarded) before the right side of the comma operator.因此,整个语句中最后一个逗号运算符左侧的所有内容都是一个丢弃值表达式,该表达式在逗号运算符右侧之前计算(并丢弃其结果)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM