Th******@cpp.com wrote:
Why does this work:
It seems that we're concatenating two literals:
std::cout << greeting + name + " Hello " + "There" << std::endl;
but if you remove (greeting + name +), it doesn't work ?
operator+ is left associative. So what you get is:
(((greeting + name)) + " Hello ") + "There"
Assuming greeting and/or name is of type std::string, then it is easy to
follow along as see that each subexpression is a std::string.
Remove the (greeting + name +) and all you are left with is trying to
add two string literals, which does not result in concatenation. If you
want to concatenate two string literals, simply put them next to each
other with no operator:
std::cout << " Hello " "There" << std::endl;
This will concatenate them AT COMPILE TIME.
--
Alan Johnson