Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of "+ +" operator (not ++) [duplicate]

Tags:

c++

Why this code even compile? What is the meaning of "+ +" operator?

#include <string>
int main()
{
  std::string c = "abc";
  c = c + + "d";
  c = c + + + "d";
  c = c + + + + "d";
  c = c + + + + + "d";
  printf("%s\n", c.c_str());
}
like image 899
Fan Yu Avatar asked Nov 16 '25 23:11

Fan Yu


2 Answers

There is no + + operator. There's a + operator (which occurs in both unary and binary forms), and a ++ operator, not used here.

Each of those is a binary + operator followed by one or more unary + operators.

This:

c = c + + "d";

is equivalent to

c = c + (+ "d");

This:

c = c + + + "d";

is equivalent to:

c = c + (+ + "d");

or:

c = c + (+ (+ "d"));

And so forth.

like image 184
Keith Thompson Avatar answered Nov 18 '25 15:11

Keith Thompson


That's just a lot of unary pluses.

like image 40
Alexey Guseynov Avatar answered Nov 18 '25 15:11

Alexey Guseynov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!