Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do temporary objects have scope?

Names have scope (a compile-time property), while objects have lifetimes (a runtime property). Right?

I often see people talking about temporary objects "going out of scope". But since a temporary object does not have a name, I think it does not make sense to talk about "scope" in this context. The lifetime of a temporary object is very clearly defined and has nothing to do with scope. Would you agree?

like image 531
fredoverflow Avatar asked Nov 24 '10 14:11

fredoverflow


People also ask

What is a temporary object in C++?

A temporary object is an unnamed object created by the compiler to store a temporary value.

Is temporary object created in return by reference?

Explanation: The temporary object is not created. If object is returned by reference, a particular memory location will be denoted with another name and hence same address values will be used.

What happens when a reference goes out of scope?

If the object is destroyed before the reference is, the reference becomes invalid (like a hanging pointer). Any attempts to access that reference result in undefined behavior.

How can you extend the lifetime of an object?

The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference (since C++11), see reference initialization for details.


2 Answers

Names have scope (a compile-time property),

Yes. I would not call it a property thought. But basically yes.

while objects have lifetimes (a runtime property). Right?

There are three types of variables. Each type has different properties in relation to lifetimes.

  • Automatic storage duration:
  • Static storage duration
  • Dynamic storage duration

Note: automatic storage duration objects have a lifetime that is bound to the scope of the variable.

I often see people talking about temporary objects "going out of scope".

Unless bound to a variable a temporary is destroyed at the end of an expression. If they are bound to a variable (a const reference) then they have the same lifespan as the variable. Sometimes it is just easier to refer to this as the scope, but technically you are correct.

But since a temporary object does not have a name, I think it does not make sense to talk about "scope" in this context.

Technically yes. But I think it just makes talking about it easier. To me (though not technically correct) the scope of a temporary (not bound) is the expression. Its easier to say than the lifespan of the temporary variable.

The lifetime of a temporary object is very clearly defined and has nothing to do with scope. Would you agree?

Yes. But it still feels more natural to talk about scope (even if it is not technically correct). As most people understand what you are trying to imply. But when you get down and talk about the very technical stuff you should use the correct terminology and scope in this context is not correct.

like image 102
Martin York Avatar answered Oct 26 '22 10:10

Martin York


The lifetime of temporaries has very little to do with syntactical blocks, but "scope" — as a word rather than a technical term — can be used in other ways. The important question is whether you are confused when people use "scope" to refer to temporaries. (It doesn't appear that you are, from my POV.)

Since you're talking about using the term to communicate with others, that communication is what's really important. If you were defining terms by writing a standardese document or trying to interpret such a document in the context of defined terms, the situation would be different. Interpreting ISO 14882 will, of course, involve communicating with others, so you would just have to ask for clarification if necessary, in that case.

It's counter-productive to make all non-standardese communication be standardese, and it's often better to use code in either case when high precision is required. The C++ standard extensively uses examples for this reason.

For another example, "calling a constructor" is often used, yet technically you can't call a ctor directly; instead, ctors are part of object initialization. This is why there's an explicit form of new solely to construct an object. (Interestingly, you can call a destructor directly.) However, I would expect that phrase to be understood in most contexts, though I wouldn't advocate using it in standardese contexts.

like image 37
Fred Nurk Avatar answered Oct 26 '22 10:10

Fred Nurk