Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does this temporary reference survive long enough?

Tags:

c++

temporary

In

template <typename T>
T const & foo(T const & dflt) 
  { return /* ... */ ? /* ... */ : dflt; }

int x = foo(5);

Does the reference to temporary "survive" until it's assigned to x?

(I know it survives the duration of the foo() call, but the assignment makes me hesitate)

The code in question has some legitimate uses for relying on const references here, i.e. I would need two separate functions

T const & foo_cr(T  const & dflt);
T const & foo_v(T dflt);

which I want to avoid (just relying on different signatures wiht the same name seems a bit risky to me.)

like image 999
peterchen Avatar asked Feb 06 '12 12:02

peterchen


People also ask

Why does Excel use so much RAM?

The reason for this is that Excel has its own memory manager and memory limits, regardless of the memory capacity of your machine. In fact all the Excel versions after Excel 2003 were designed to use a maximum of 2GB memory. So while your computer may have 4GB or even 8GB RAM, Excel can only use 2GB of that.

When should you not use garbage collection?

One fundamental problem with garbage collection, though, is that it is difficult to estimate and manage the actual size of the working set in memory, because garbage collector can free your memory only delayedly. So, yes, when memory is restricted, garbage collection might not be a good choice.

How does GC collect work?

It performs a blocking garbage collection of all generations. All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected. Use this method to force the system to try to reclaim the maximum amount of available memory.

Does C# have garbage collection?

The C# garbage collection uses three generations in total: Generation 0—This generation holds short-lived objects. Here's where the collection process happens most often.


1 Answers

Does the reference to temporary "survive" until it's assigned to x?

Generally, yes. If foo returns dflt, that reference (to a temporary 5) is valid until the end of the full-expression it was created in, that means, until after the initialization.

C++03, 12.2/3:

When an implementation introduces a temporary object of a class that has a non-trivial constructor (12.1), it shall ensure that a constructor is called for the temporary object. Similarly, the destructor shall be called for a temporary with a non-trivial destructor (12.4). Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception.

More comes in 12.2/4 and 12.2/5, especially the exceptions of this rule.

I can't see what happens when it returns the ... you left out, but I guess you weren't asking about this.

like image 126
jpalecek Avatar answered Nov 03 '22 09:11

jpalecek