简体   繁体   中英

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 :).

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?

Without something to add to the addition operator just means positive so +2; just means (+2); which is like just having a line 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.

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:

warning: expression result unused [-Wunused-value]

 + 7; ^ ~ 

We can get the same warning in Visual Studio using /Wall :

warning C4555: expression has no effect; expected expression with side-effect

This is covered in the draft C++ standard section 5.3.1 Unary operators which says:

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:

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.

+ 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). 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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