I discovered a reference lifetime extension gotcha.
TL;DR; Chained functions (that return a reference to
It works even when a direct public member of the temporary object is assigned to a const reference. For example,
By the time the
Clang address sanitizer did not catch it either. It looks like a case of UseAfterFree but there's no
So, the following is NOT a good solution.
Would you really accept it as a solution? .... It's too verbose!
TL;DR; Chained functions (that return a reference to
*this) 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 return *this by-value; Third, use rvalue reference qualified overloads and have only them return by-value; Fourth, have a last chained ExtendLifetime() function that returns a prvalue (of type *this).
C++ Reference Lifetime Extension
C++ has a feature called "reference lifetime extension". Consider the following.
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.
The temporary std::array returned by create_array_of_strings() is not destroyed after the function returns. Instead the "lifetime" of the temporary std::array is extended till the end of lifetime of reference arr. This feature saves you from coping the object when all you want to do is to inspect it's state. Without this feature, the std::array must be copied in a local variable. A move would be attempted but move-constructor for std::array just 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,
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
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.- Any access to the nested state via a function (member or free) disables lifetime extension of the parent. For example
Person().GetName().first_namewould no longer trigger lifetime extension of the temporaryPerson. - As a corollary, any conversion member functions, chained member functions do not extend the lifetime of the original temporary.
Chained Functions Break Lifetime Extension
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 classPerson and class 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; }
};
Such a chainable class could be used to create a person in place.
Person p = Person().FirstName("C").LastName("Z").MailingAddress(Address().Line1("...").Line2("...").State("CA").Zip(12345));
As the line is too long, one might attempt a straight-forward refactoring---only to be chastised after.
auto &addr = Address().Line1("...").Line2("...").State("CA").Zip(12345);
Person p = Person().FirstName("C").LastName("Z").MailingAddress(std::move(addr));
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 the
MailingAddress function is called, the addr has become a dangling reference. The Address object is already created and destroyed. The reason is that the lifetime of the temporary Address object 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 no
malloc/free in sight.
Restoring Extended Lifetime
There're a couple of ways to restore lifetime extension.- 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
*thisby-value.
*this by-value from each function is expensive. Compiler makes a copy of *this to construct the returned object. Move semantics don't kick in because a member function that returns *this by-value cannot automatically decide to move from *this. There could be a legitimate lvalue pointing to *this.
So, the following is NOT a good solution.
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?
rvalue reference qualified functions could be used to avoid a copy. Note the difference in return types and reference qualifications.
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); }
};
The above 2x verbose Address class is one solution to the lifetime extension problem. An lvalue Address remains an lvalue throughout chaining. An rvalue Address remains an rvalue throughout chaining. The rvalue qualified member functions don't make a copy either. They move. Hopefully, the Address class has fast move-semantics. Not all classes do.
Would you really accept it as a solution? .... It's too verbose!
Dedicated Lifetime Extending Function
Second option is to add a function that terminates the chain and extends the lifetime of the original object.
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); }
};
There's ExtendLifetime function that allows us to opt into lvalue to rvalue conversion with a single move at the end of the chain. Safe code now looks like
const auto &addr = Address().Line1("...").State("CA").Zip(12345).ExtendLifetime();
Person p = Person().FirstName("C").LastName("Z").MailingAddress(addr);
The catch is that one has to remember to call the ExtendLifetime function when refactoring. It is easy to miss.
Copy/Move?
Trickiness of the issue perhaps suggests to create a copy but that may not be always possible/desirable.
auto addr = Address().Line1("...").State("CA").Zip(12345); // makes a copy
Person p = Person().FirstName("C").LastName("Z").MailingAddress(addr);
For instance, the above approach does not work if Address is move-only (e.g., contains a unique_ptr). Of course, one could use a move.
auto addr = std::move(Address().Line1("...").State("CA").Zip(12345));
Person p = Person().FirstName("C").LastName("Z").MailingAddress(std::move(addr));
Mechanics wise, this is not a whole lot different from .ExtendLifetime(). std::move is well understood. However, it kills the flow of chained functions.
Comments