jeudi 23 avril 2015

Returning by value to rvalue reference

I'm studying rvalue references and I have a doubt in the following code:

string func() {
    return "Paul";
}

int main()
{
    string&& nodanger = func();
    // The lifetime of the temporary is extended
    // to the life-time of the reference.
    return 0;
}

The question is: what does func() return?

I believe this is what happens:

  • func returns a prvalue "Paul" (is this a const char * due to a rvalue->pointer conversion?)
  • a string object is implicitly constructed (which ctor is used?)
  • due to reference collapsing rules it is bound to "nodanger" (does this behave any differently from a string& normal reference?)

Aucun commentaire:

Enregistrer un commentaire