7

I'm revising a proposal to expand the C++ feature where "temporary" objects cease to be temporary when attached to a name by binding a reference. Here's a sample of the classic behavior (not the proposal):

int && i = 3 + 3; // Calculate 3+3 and give a name to the result.
int j = 3 + 3; // Create a named variable, calculate 3 + 3, and copy the result in.

// Similar outcomes, but not exactly the same when using more complex types:

struct haz_int {
    int member;
    ~ haz_int() { std::cout << "No haz!\n"; }
};
int value = has_int{ 3 }.member; // Create a haz_int, copy its member out.
int && value = haz_int{ 3 }.member; // Retain entire haz_int and name its member.

Who invented temporary lifetime extension, and why? D&E, the 1994 retrospective by language inventor Stroustrup, doesn't mention it in its section on the topic. According to D&E, the lifetime of temporary objects was contentious during the early standardization process, so it's a little surprising it was reopened and tweaked. On the other hand, it's possible that he neglected to mention it as a minor detail. This seems doubtful though because his stated purpose in that section is:

I will present two issues, name lookup and lifetime of temporaries, to illustrate the difficult and detailed work done. The majority of the committee's efforts are expended on such issues.

The earliest working draft of the standard posted on the ISO working group site, from September 1993, says (§8.4.3 References),

… if and only if the reference is to a const and an object of type T can be created from the initializer, such an object will be created. The reference then becomes a name for that object.

The lifetime of a temporary object created in this way is the scope in which it is created (3.5). Note that a reference to a class B can be initialized by an object of a class D provided B is an accessible and unambiguous base class of D (in that case a D is a B); see 4.7.


Update:

The proposal paper archives go back further. From July 1993, N0305 by Dag Brück is very similar to Stroustrup's summary of the debate in D&E. It mentions:

Also note that a reference may be bound to a temporary, in which case the temporary has the lifetime of the enclosing scope, see Section 8.4.3 of the Working Paper.

So, apparently the lifetime extension rule predates the general rule for the lifetime of temporaries!

6

N0345 Lifetime of temporaries explicitly bound to references, by John Bruns and from September 1993, is what fleshed out the details of lifetime extension.

The paper briefly mentions a motivation for the preexisting, simpler form of lifetime extension, which is to avoid copying class objects. It also mentions reliably letting function arguments be refactored into named objects.

It proposes a stronger rule than the one ultimately adopted:

For any statement explicitly binding a reference to a temporary, the lifetime of all temporaries in the statement are extended to match the lifetime of the reference.

Perhaps my proposal will find support, being in the middle ground between this original ideal and the status quo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.