How to understand the following std::string
init syntax?
#include <iostream>
#include <string>
int main ()
{
std::string y;
std::string x = "x str";
new (&y) std::string(x);
std::cout << y << std::endl;
return 0;
}
Output:
x str
Can we split the statement into 2 steps?
1.
string* temp = new std::string(x);
2.
(&y) = temp
So the original statement is just a shortcut for step 1 + 2.
Reference:
1. https://en.cppreference.com/w/cpp/string/basic_string/basic_string