[英]Why does compiler not balk at a line beginning with an addition operator?
I was just doing some refactoring, and created a bug for myself: 我只是做了一些重构,并为自己创建了一个错误:
int i = 2;
+ 7;
+ 4;
This is simplified; 这是简化的; the additional semicolons at the ends of lines 1 & 2 were not quite this obvious - at least to me :).
第1行和第2行末尾的附加分号并不是那么明显 - 至少对我来说:)。
What I cannot figure out is why the compiler didn't catch it. 我无法弄清楚为什么编译器没有抓住它。 Is there some valid action in C++ which begins a line with an addition operator?
在C ++中是否有一些有效的操作,它开始使用加法运算符?
Without something to add to the addition operator just means positive so +2;
没有添加到添加运算符的东西只是意味着正
+2;
just means (+2);
只是意味着
(+2);
which is like just having a line i;
这就像只有一条线
i;
or similar. 或类似的。 Nothing 'wrong' with it, but nothing will happen either.
没有什么“错误”,但也没有任何事情发生。 If you compile under *nix with gcc with -Wall specified you'll get the error
warning: statement has no effect
which is generally good to know because it is often a sign a statement you have intended to do something is in fact not doing what it was supposed to. 如果你使用带有-Wall指定的gcc在* nix下编译你将得到错误
warning: statement has no effect
通常很好知道,因为它通常是一个标志你想要做某事的声明其实不是做什么的它应该是。
This is a completely valid code you are using the unary + operator , the result is the value of the operand, it also performs the integer promotions on the operand. 这是一个完全有效的代码,您使用的是一元+运算符 ,结果是操作数的值,它还对操作数执行整数提升。
Turing on warning would have been helpful in this case, for example gcc and clang using -Wall -Wextra
would give you a warning like this: 在这种情况下发出警告会有所帮助,例如使用
-Wall -Wextra
gcc和clang会给你一个这样的警告:
warning: expression result unused [-Wunused-value]
警告:表达式结果未使用[-Wunused-value]
+ 7; ^ ~
We can get the same warning in Visual Studio using /Wall : 我们可以使用/ Wall在Visual Studio中获得相同的警告:
warning C4555: expression has no effect;
警告C4555:表达无效; expected expression with side-effect
具有副作用的预期表达
This is covered in the draft C++ standard section 5.3.1
Unary operators which says: 这是覆盖在C ++草案标准节
5.3.1
元运算符它说:
The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument.
一元+运算符的操作数应具有算术,无范围枚举或指针类型,结果是参数的值。 Integral promotion is performed on integral or enumeration operands.
对整数或枚举操作数执行整体提升。 The type of the result is the type of the promoted operand.
结果的类型是提升的操作数的类型。
cppreference has the following to say: cppreference有以下说法:
The builtin unary plus operator returns the value of its operand.
内置的一元加运算符返回其操作数的值。 The only situation where it is not a no-op is when the operand has integral type or unscoped enumeration type, which is changed by integral promotion, eg, it converts char to int or if the operand is subject to lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion.
它不是无操作的唯一情况是当操作数具有整数类型或未整合的枚举类型时,它由整数提升改变,例如,它将char转换为int或者如果操作数受到左值到右值的影响,数组到指针或函数到指针的转换。
+ 7;
is an expression statement , which consists of an expression followed by a semicolon. 是一个表达式语句 ,它由一个表达式后跟一个分号组成。
The expression is evaluated, and the result is discarded. 计算表达式,并丢弃结果。 Usually this is done because the expression has side effects (such as an assignment or an I/O statement).
通常这样做是因为表达式具有副作用(例如赋值或I / O语句)。 An expression statement where the expression has no side effects is legal but useless.
表达式没有副作用的表达式语句是合法的但没用。
Some compilers might warn about it if you ask them nicely. 如果你很好地问他们,一些编译器可能会警告它。
As others have pointed out, +
is the unary plus operator, which exists for symmetry with the unary -
operator. 正如其他人所指出的那样,
+
是一元加运算,其存在是为了与一元对称-
运营商。 It yields the value of its operand (after performing integral promotions when appropriate). 它产生其操作数的值(在适当的情况下执行整数提升后)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.