If there is a difference between two constructs I would like to know
std::string name = std::string("Eugene"); and
std::string name = "Eugene"; First lets consider the statement:
std::string name = std::string("Eugene"); For the above shown statement there are 2 possibilities in C++11.
std::string is created using "Eugene" on the right hand side. Then, the copy/move constructor of std::string is used to construct the object named name.name, implementations can just directly construct name from "Eugene".Now lets consider the statement:
std::string name = "Eugene"; //this is initialization In the above statement, an object named name is constructed using the string literal and a suitable std::string's constructor.
So, the answer to your question in C++11 is that there is a difference between the two statements only if the temporary is not elided.
In C++17, there is mandatory copy elison which means that in this case when we write:
std::string name = std::string("Eugene"); In the above statement, the language guarantees that
No temporary on the right hand side is created. Instead, the object
nameis created directly using the string literal"Eugene"and a suitablestd::string's constructor.
So the answer to your question in C++17 is that there is no difference between the two statements.
std::string name = "Eugene";it might look like an assignment, but it's not. It's a constructor call.std::string("Eugene")to construct a temporarystd::stringusing the literal"Eugene", a copy constructor to constructnamefrom that temporary, and the temporary then ceases to exist. In the second, name is constructed directly from the literal"Eugene". The wrinkle is that the implementation is explicitly permitted but not required to elide the temporary (i.e. never create it) and, for implementations which do that, the two cases are equivalent. In C++17, the elision became mandatory, so the two cases are equivalent.