88
(int) + 4*5;

Why is this (adding a type with a value) possible? (tried with g++ and gcc.)

I know that it doesn't make sense (and has no effect), but I want to know why this is possible.

zvavybir
  • 1,034
  • 6
  • 15
  • 19
    same as `(int)-4*5` – 0___________ Apr 21 '19 at 15:29
  • 47
    There is a useful tool called `cppinsights` that helps to understand how the code looks from the compiler frontend perspective. It also has an online version, you can see what it tells about [your example](https://cppinsights.io/lnk?code=aW50IG1haW4oKQp7CgkoaW50KSArIDQqNTsKfQ==&insightsOptions=cpp17&std=cpp17&rev=1.0) (the same 'parenthesization' as the answers your were given) – Nikita Kniazev Apr 21 '19 at 21:02
  • 22
    This statement is equivalent to `+(int)+ 4*5;` and `-(int)- 4*5;` and `-+-+-(int)-+-+- 4*5;` and less poetically `;` – chqrlie Apr 22 '19 at 00:12
  • @Ernest Bredar: The thing is, typecasting makes perfect sense when used in an expression, e.g. "num = (int) + 4*5" (even though it's redundant in this particular case). But why can you have an expression without an assignment? And gcc doesn't complain if the expression is just "+4 * 5;" – jamesqf Apr 22 '19 at 04:32
  • 16
    What part puzzles you? For all I know, you are asking why you are allowed to write 5 without indicating the sign. – Carsten S Apr 22 '19 at 06:26
  • 2
    Possible duplicate of [What does the unary plus operator do?](https://stackoverflow.com/questions/727516/what-does-the-unary-plus-operator-do) – Andrey Tyukin Apr 22 '19 at 07:46
  • 2
    @jamesqf *why can you have an expression without an assignment?* So you can call functions that return a value that you don't need without having to do a pointless assignment. Would you prefer to be forced to write `char_count = printf("something\n");`? – PM 2Ring Apr 22 '19 at 08:39
  • 5
    Shouldn't C++ warn you that C-type casts are not recommended in C++? – Mr Lister Apr 22 '19 at 09:38
  • 1
    @PM 2Ring: A function call is not the same as an expression, and a compiler had better be able to distinguish between the two :-) – jamesqf Apr 22 '19 at 15:52
  • 3
    @jamesqf It's a kind of expression, i.e., all function calls are expressions, but not vice versa. So why should the language definition permit unassigned function calls, but not others? Also, an assignment is itself an expression, which allows chained assignments, but the chain has to stop somewhere. But here's a simpler example: `i++;` – PM 2Ring Apr 22 '19 at 17:14
  • 5
    No, @MrLister. They're perfectly legal in C++, and part of the language specification. Certain compilers have optional warnings for this (like GCC's `-Wold-style-cast`), but they're more like code *style* warnings (a la Lint) than actual compiler warnings. Microsoft's compiler has no such warning, although it does have some style/code analysis tools that might be able to be configured accordingly. – Cody Gray - on strike Apr 22 '19 at 17:53
  • 3
    This question was reposted without attribution on Quora: https://www.quora.com/Why-do-C-and-C-allow-the-expression-int-4-5 – Keith Thompson Apr 22 '19 at 19:53

2 Answers2

142

The + here is unary + operator, not the binary addition operator. There's no addition happening here.

Also, the syntax (int) is used for typecasting.

You can re-read that statement as

(int) (+ 4) * 5;    

which is parsed as

((int) (+ 4)) * (5);    

which says,

  1. Apply the unary + operator on the integer constant value 4.
  2. typecast to an int
  3. multiply with operand 5

This is similar to (int) (- 4) * (5);, where the usage of the unary operator is more familiar.

In your case, the unary + and the cast to int - both are redundant.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 52
    "Casting", not "typecasting". Typecasting is something that happens to actors. – Keith Thompson Apr 21 '19 at 23:41
  • 9
    `(+ 4)` is not *make the operand `+4`*, it means apply the unary `+` to operand `4`, which indeed is a no-op in the OP's case, but could cause integer promotion or array decay in other circumstances. For example `char c = 0; sizeof +c == sizeof c` is probably false and `sizeof +"a"` is probably not 2. – chqrlie Apr 22 '19 at 00:07
  • 7
    "both are redundant" - the whole thing is redundant, just like `42;` :-) – paxdiablo Apr 22 '19 at 08:17
  • 11
    I see nothing wrong with using the term Type Casting. It seems [I'm not the only one](http://www.cplusplus.com/doc/oldtutorial/typecasting/). – Ben Apr 22 '19 at 15:28
  • 14
    @Ben Typecasting is not type casting. – Kenneth K. Apr 22 '19 at 17:05
  • 3
    Furthermore, the past participle of "cast" is "cast", [unless you're in the 1500s](https://english.stackexchange.com/questions/94565/can-casted-be-the-past-tense-of-cast). – osuka_ Apr 22 '19 at 21:34
  • 7
    The word "typecasting" is _completely_ and _entirely_ understood in this context. Why would anyone think it relates to actors? What pointless bikeshedding! – Lightness Races in Orbit Apr 23 '19 at 11:22
  • Don't the number constants have a type? – Peter Mortensen Apr 23 '19 at 13:35
  • 1
    @PeterMortensen They do. Here it'd be `int`. – Sourav Ghosh Apr 23 '19 at 13:35
41

This is interpreted as ((int)(+4)) * 5. That is, an expression +4 (a unary plus operator applied to a literal 4), cast to type int with a C-style cast, and the result multiplied by 5.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85