int a = 1;
int b = (1,2,3);
cout << a+b << endl; // this prints 4
- Is
(1,2,3)
some sort of structure in c++ (some primitive type of list, maybe?) - Why is
b
assigned the value3
? Does the compiler simply take the last value from the list?
(1,2,3)是c ++中的某种结构(某种原始类型的列表,也许?)
为什么b的值为3?编译器是否只是从列表中取出最后一个值?
5 个解决方案
#1
8
Yes, that's exactly it: the compiler takes the last value. That's the comma operator, and it evaluates its operands left-to-right and returns the rightmost one. It also resolves left-to-right. Why anyone would write code like that, I have no idea :)
是的,就是这样:编译器获取最后一个值。这是逗号运算符,它从左到右评估其操作数并返回最右边的操作数。它也从左到右解决。为什么有人会写这样的代码,我不知道:)
So int b = (1, 2, 3)
is equivalent to int b = 3
. It's not a primitive list of any kind, and the comma operator ,
is mostly used to evaluate multiple commands in the context of one expression, like a += 5, b += 4, c += 3, d += 2, e += 1, f
for example.
所以int b =(1,2,3)等价于int b = 3.它不是任何类型的原始列表,而逗号运算符主要用于在一个表达式的上下文中计算多个命令,如+例如,= 5,b + = 4,c + = 3,d + = 2,e + = 1,f。
#2
6
(1,2,3)
is an expression using two instances of the comma operator. The comma operator evaluates its left operand, then there's a sequence point and then it evaluates its right operand. The value of the comma operator is the result of the evaluation of the right hand operand, the result of evaluating the left operand is discarded.
(1,2,3)是使用逗号运算符的两个实例的表达式。逗号运算符计算其左操作数,然后是一个序列点,然后它计算它的右操作数。逗号运算符的值是右手操作数的计算结果,评估左操作数的结果被丢弃。
int b = (1,2,3);
is, therefore, equivalent to:
因此,相当于:
int b = 3;
Most compilers will warn about such a use of the comma operand as there is only ever a point to using a comma operator if the left hand expression has some side effect.
大多数编译器会警告这样使用逗号操作数,因为如果左手表达式有一些副作用,则只使用逗号运算符。
#3
4
The second line is using the comma operator. An expression like
第二行是使用逗号运算符。一个表达式
a, b
evaluates both a
and b
and returns b
.
评估a和b并返回b。
In this case, the second line is parsed like:
在这种情况下,第二行解析如下:
int b = ((1, 2), 3);
so (1, 2)
is evaluated (to 2
) then thrown away, and the end result is simply 3.
所以(1,2)被评估(到2)然后扔掉,最终结果就是3。
#4
1
It is the comma operator which takes the form expr, expr
. The first expression is evaluated and the result is discarded, the second expression is evaluated and its result is returned.
它是逗号运算符,其形式为expr,expr。计算第一个表达式并丢弃结果,计算第二个表达式并返回其结果。
In your case that line is evaluated as:
在您的情况下,该行被评估为:
((1, 2), 3) =>
(2, 3) =>
3
#5
-3
it probably used to be b = foo(1,2,3) but the foo got deleted accidentally. C doesn't complain about this kind of crap "it's a feature".
它可能曾经是b = foo(1,2,3),但foo被意外删除了。 C不会抱怨这种废话“这是一个特色”。
#1
8
Yes, that's exactly it: the compiler takes the last value. That's the comma operator, and it evaluates its operands left-to-right and returns the rightmost one. It also resolves left-to-right. Why anyone would write code like that, I have no idea :)
是的,就是这样:编译器获取最后一个值。这是逗号运算符,它从左到右评估其操作数并返回最右边的操作数。它也从左到右解决。为什么有人会写这样的代码,我不知道:)
So int b = (1, 2, 3)
is equivalent to int b = 3
. It's not a primitive list of any kind, and the comma operator ,
is mostly used to evaluate multiple commands in the context of one expression, like a += 5, b += 4, c += 3, d += 2, e += 1, f
for example.
所以int b =(1,2,3)等价于int b = 3.它不是任何类型的原始列表,而逗号运算符主要用于在一个表达式的上下文中计算多个命令,如+例如,= 5,b + = 4,c + = 3,d + = 2,e + = 1,f。
#2
6
(1,2,3)
is an expression using two instances of the comma operator. The comma operator evaluates its left operand, then there's a sequence point and then it evaluates its right operand. The value of the comma operator is the result of the evaluation of the right hand operand, the result of evaluating the left operand is discarded.
(1,2,3)是使用逗号运算符的两个实例的表达式。逗号运算符计算其左操作数,然后是一个序列点,然后它计算它的右操作数。逗号运算符的值是右手操作数的计算结果,评估左操作数的结果被丢弃。
int b = (1,2,3);
is, therefore, equivalent to:
因此,相当于:
int b = 3;
Most compilers will warn about such a use of the comma operand as there is only ever a point to using a comma operator if the left hand expression has some side effect.
大多数编译器会警告这样使用逗号操作数,因为如果左手表达式有一些副作用,则只使用逗号运算符。
#3
4
The second line is using the comma operator. An expression like
第二行是使用逗号运算符。一个表达式
a, b
evaluates both a
and b
and returns b
.
评估a和b并返回b。
In this case, the second line is parsed like:
在这种情况下,第二行解析如下:
int b = ((1, 2), 3);
so (1, 2)
is evaluated (to 2
) then thrown away, and the end result is simply 3.
所以(1,2)被评估(到2)然后扔掉,最终结果就是3。
#4
1
It is the comma operator which takes the form expr, expr
. The first expression is evaluated and the result is discarded, the second expression is evaluated and its result is returned.
它是逗号运算符,其形式为expr,expr。计算第一个表达式并丢弃结果,计算第二个表达式并返回其结果。
In your case that line is evaluated as:
在您的情况下,该行被评估为:
((1, 2), 3) =>
(2, 3) =>
3
#5
-3
it probably used to be b = foo(1,2,3) but the foo got deleted accidentally. C doesn't complain about this kind of crap "it's a feature".
它可能曾经是b = foo(1,2,3),但foo被意外删除了。 C不会抱怨这种废话“这是一个特色”。