0

If there is a difference between two constructs I would like to know

std::string name = std::string("Eugene"); 

and

std::string name = "Eugene"; 
8
  • Assuming C++17 or later: no, they are the same except for more typing in the first case. Commented Jan 14, 2022 at 23:04
  • that is, the compiler, in the absence of an explicit call to std::string(), will call it for me? Commented Jan 14, 2022 at 23:05
  • 1
    Some good reading on the topic. And here's some good viewing. Commented Jan 14, 2022 at 23:08
  • 1
    @MaskOryle no. But this is: std::string name = "Eugene"; it might look like an assignment, but it's not. It's a constructor call. Commented Jan 14, 2022 at 23:42
  • 2
    Before C++17, there is a semantic difference. The first uses std::string("Eugene") to construct a temporary std::string using the literal "Eugene", a copy constructor to construct name from 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. Commented Jan 15, 2022 at 0:01

1 Answer 1

2

C++11

First lets consider the statement:

std::string name = std::string("Eugene"); 

For the above shown statement there are 2 possibilities in C++11.

  1. A temporary object of type 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.
  2. In C++11, there is non-mandatory copy elision which means that implementations are allowed to elide the temporary on the right hand side. This means instead of creating a temporary on the right hand side and then using a copy/move constructor to construct 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.

C++17

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 name is created directly using the string literal "Eugene" and a suitable std::string's constructor.

So the answer to your question in C++17 is that there is no difference between the two statements.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.