*this

*this

ExtendLifetime()

C++ Reference Lifetime Extension

std::array<std::string, 5> create_array_of_strings(); { const std::array &arr = create_array_of_strings(); // Only inspect arr here. } // arr out of scope. The temporary pointed to by arr destroyed here.

std::array

create_array_of_strings()

std::array

arr

std::array

std::array

struct Person { struct Name { std::string first_name; std::string last_name; } name; }; Person birth(); { const std::string &first_name = birth().name.first_name; // do something with first_name. } // first_name out of scope. The referred Person went to grave here.

Gotchas

Any access to the nested state via a function (member or free) disables lifetime extension of the parent. For example Person().GetName().first_name would no longer trigger lifetime extension of the temporary Person . As a corollary, any conversion member functions, chained member functions do not extend the lifetime of the original temporary.

Chained Functions Break Lifetime Extension

Person

Address

class Address { public: Address &Line1(std::string l1) { ... return *this; } Address &Line2(std::string l2) { ... return *this; } Address &State(std::string state) { ... return *this; } Address &Zip(int zip) { ... return *this; } }; class Person { public: Person &FirstName(std::string first) { ... return *this; } Person &LastName(std::string first) { ... return *this; } Person &MailingAddress(Address addr) { ... return *this; } };

Person p = Person().FirstName("C").LastName("Z").MailingAddress(Address().Line1("...").Line2("...").State("CA").Zip(12345));

auto &addr = Address().Line1("...").Line2("...").State("CA").Zip(12345); Person p = Person().FirstName("C").LastName("Z").MailingAddress(std::move(addr));

MailingAddress

addr

Address

Address

malloc/free

Restoring Extended Lifetime

Return the container class ( Person, Address ) by-value from each member function. This option is either very expensive or 2x verbose. Add a dedicated chain termination function that returns *this by-value.

*this

*this

*this

*this

*this

class Address { // Each chained function makes a copy public: Address Line1(std::string l1) { ... return *this; } Address Line2(std::string l2) { ... return *this; } Address State(std::string state) { ... return *this; } Address Zip(int zip) { ... return *this; } };

Rvalue Reference Qualified Functions?

class Address { // No longer makes a copy when *this is an rvalue. public: Address & Line1(std::string l1) & { ... return *this; } Address Line1(std::string l1) && { ... return std::move(*this); } Address & Line2(std::string l2) & { ... return *this; } Address Line2(std::string l2) && { ... return std::move(*this); } Address & State(std::string state) & { ... return *this; } Address State(std::string state) && { ... return std::move(*this); } Address & Zip(int zip) & { ... return *this; } Address Zip(int zip) && { ... return std::move(*this); } };

Address

Address

Address

Address

Dedicated Lifetime Extending Function

class Address { // Each chained function makes a copy public: Address & Line1(std::string l1) { ... return *this; } Address & Line2(std::string l2) { ... return *this; } Address & State(std::string state) { ... return *this; } Address & Zip(int zip) { ... return *this; } Address ExtendLifetime() & { return std::move(*this); } };

ExtendLifetime

const auto &addr = Address().Line1("...").State("CA").Zip(12345).ExtendLifetime(); Person p = Person().FirstName("C").LastName("Z").MailingAddress(addr);

ExtendLifetime

Copy/Move?

auto addr = Address().Line1("...").State("CA").Zip(12345); // makes a copy Person p = Person().FirstName("C").LastName("Z").MailingAddress(addr);

Address

unique_ptr

auto addr = std::move(Address().Line1("...").State("CA").Zip(12345)); Person p = Person().FirstName("C").LastName("Z").MailingAddress(std::move(addr));

.ExtendLifetime()

std::move

Conclusion

I discovered a reference lifetime extension gotcha.Chained functions (that return a reference to) do not trigger C++ reference lifetime extension. Four ways out: First, don't rely on lifetime extension---make a copy; Second, have all chained functions returnby-value; Third, use rvalue reference qualified overloads and have only them return by-value; Fourth, have a last chainedfunction that returns a prvalue (of type *this).C++ has a feature called "reference lifetime extension". Consider the following.The temporaryreturned byis not destroyed after the function returns. Instead the "lifetime" of the temporaryis extended till the end of lifetime of reference. This feature saves you from coping the object when all you want to do is to inspect it's state. Without this feature, themust be copied in a local variable. A move would be attempted but move-constructor forjust copies rhs to lhs. So this feature has been helpful since pre-move-semantics days of C++.It works even when a direct public member of the temporary object is assigned to a const reference. For example,What makes this feature interesting is all the natural-looking cases where it does not work. Abseil tip #107 mentions a few of them. For readability, I'm gonna mention them briefly here.Chaining of function is a common pattern where the state of the same object is modified by successively calling different member functions. It's known by different names including the "builder" pattern (not GoF) and Named Parameter Idiom . It would look something like this for classand classSuch a chainable class could be used to create a person in place.As the line is too long, one might attempt a straight-forward refactoring---only to be chastised after.This refactoring is incorrect. If you are lucky the code will barf at runtime soon. No guarantees though. It's UB land.By the time thefunction is called, thehas become a dangling reference. Theobject is already created and destroyed. The reason is that the lifetime of the temporaryobject is no longer extended due to the chain of member functions.Clang address sanitizer did not catch it either. It looks like a case of UseAfterFree but there's noin sight.There're a couple of ways to restore lifetime extension.Lets consider option #1. Returningby-value from each function is expensive. Compiler makes a copy ofto construct the returned object. Move semantics don't kick in because a member function that returnsby-value cannot automatically decide to move from. There could be a legitimate lvalue pointing toSo, the following is NOT a good solution.rvalue reference qualified functions could be used to avoid a copy. Note the difference in return types and reference qualifications.The above 2x verboseclass is one solution to the lifetime extension problem. An lvalueremains an lvalue throughout chaining. An rvalueremains an rvalue throughout chaining. The rvalue qualified member functions don't make a copy either. They move. Hopefully, theclass has fast move-semantics. Not all classes do.Would you really accept it as a solution? .... It's too verbose!Second option is to add a function that terminates the chain and extends the lifetime of the original object.There'sfunction that allows us to opt into lvalue to rvalue conversion with a single move at the end of the chain. Safe code now looks likeThe catch is that one has to remember to call thefunction when refactoring. It is easy to miss.Trickiness of the issue perhaps suggests to create a copy but that may not be always possible/desirable.For instance, the above approach does not work ifis move-only (e.g., contains a). Of course, one could use a move.Mechanics wise, this is not a whole lot different fromis well understood. However, it kills the flow of chained functions.So no solution appears to be perfect. The first one is massively verbose but it's correct post refactoring. The second one is also correct but the gotcha remains. Using a copy is preferred by redditors here . Some real-life libraries that may be subject to this issue are mentioned in another reddit discussion