This is an interesting case. I'm not sure if you've actually printed the result of your first statement or not, but if you do you see that you are actually getting "STING"
and not "TESTING2"
, or something like that!
There are a few things going on here. In your first statement, you are constructing a string using a string literal. In this case, C++ create a const char[N]
and put your string into it. Since char[N]
is technically an array (and array is a pointer), the operator +
acts as increment and the result of "TESTING" + 2
is actually a pointer to the second element of "TESTING"
, which is "STING"
. You'll not see any errors, because C++ thinks that what's you wanted to do.
In your second statement, you are telling C++ that I want a std::string
object. The std::string
is by design safer and will nag at you if you try to add anything other than a string to it. So, when you do, you get an error. For your second statement to work as intended, you should write t + std::to_string(2)
.