mardi 28 juillet 2015

C++11 rvalue references and variable initialisations

Let's consider the following piece of code:

std::string&& rvalue_reference_1 = std::string("some string");
std::string   regular_string("some string");

std::string&& rvalue_reference_2 = "some string";

  1. Are the first and the second statements behaving the same way ? I understand the first one as constructing a temporary object, and then simply "assigning a name" to it. Thus it becomes a regular object. Am I wrong somewhere ?

  2. In the third statement, I'm not sure to understand what happens under the hood. "some string" is an anonymous/temporary const char*, and rvalue_reference_2 a rvalue reference to a std::string object, not a rvalue reference to a const char*... By writing std::string&&, do we implicitly call std::string's constructor?

Aucun commentaire:

Enregistrer un commentaire