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());
}
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());
}
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.
The first +
is a binary plus which calculates the sum of c
and the second term.
The remaining +
are unary plus operators. In + "d"
, "d"
is of type const char[2]
, and decays to const char*
. Then +
is applied to the pointer which has no effect, and returns the same const char*
.
c + + + "d"
is equivalent to c + (+(+"d"))
.