A: 

I don't think you can do this with current C++. You need the move semantics that will be introduced in C++x0.

alex tingle
Is the C++x0 intentional or a typo?
Naveen
It's the sad true :(
Pavel Shved
martin
I think you mean C++0x
Bill
Well, they've got 3 months to prove me wrong.
alex tingle
Ahh, that's cute :)
Bill
+2  A: 

The original Guru of the Week article is here: Guru of the Week #15.

Part of your answer might come from Herb himself, in "A candidate for the most important const" - the "const" part of assigning a temporary to a reference is indeed important.

So it would appear as if this is a bug in the original article.

MadKeithV
Thanks for the link; yes, that clarifies point 1. in my post. However, as stated, I consider more important he problem of the temporary dying with a valid reference still attached applies even if we trade the reference it for a const-reference (as in my code). Also I don't have a problem if that is illegal, even though I wonder why the C++ Standard would differentiate between the local and the class embedded reference.It would be just good to know if the compiler a) can detect that error and b) if it even should try to detect it or if it's something programmer's just have to keep in mind.
martin
+1  A: 

I always considered that passing address parameters (whether by reference or pointer) required an understanding about the lifetime of the thing passed. Period. End of Discussion. This seems just an attribute of an un-garbage-collected/NOT-reference-managed environment.

This is one of the benefits of GC.

In c++ sometimes the lifetime issues were solved by:

X::clone

or by explicit documentation of the interface eg:

"it is up to the instantiator of Y to ensure that the instance of X passed in parameter x remains in existence for the entire lifetime of Y"

or by

explicit copying of x in the guts of the receiver.

That's just c++. That c++ added a guarantee on const references was nice (and really somewhat necessary) but that was that.

Thus I consider the code as shown in the question to be wrong and am of the opinion it should have been:

int main()
{
  MyWorker w;
  GenericTableAlgorithm a( "Customer", w);
  a.Process();
}
pgast
Yes, that was what I was thinking, too. I personally put it down to an oversight in the GotW article, since Sutter evolves the example to different solutions later which are based on (template) functions instead of class-objects and naturally have no such problem. I was reminded of code that uses temporary functors this way, where it works since canonically functors have value semantics; perhaps this was also the case with Mr. Sutter.In any case I agree with your solution, even though we have the (minor) stain of w being visible and never used again for the rest of main;can't have everything.
martin
of course one might have written GenericTableAlgorithm("Customer",MyWorker()).Process(); relying on the "full expression" part of the c++ temporary lifetime statement. Perhaps an invitation for someone to go in and change and create havoc. ;-)
pgast
+1  A: 

I think this is a tricky part that is not too clear. There was a similar question just a couple of days ago.

By default temporaries are destroyed in reverse order of construction when the full-expression in which they were created completes. Up to here everything is fine and understood, but then exceptions arise (12.2 [class.temporary]/4,5) and things become confusing.

Instead of dealing with the exact wording and definitions in the standard I will approach the problem from an engineering / compiler perspective. Temporaries are created in the stack, when a function completes the stack frame is freed (the stack pointer is moved back to the original position before the function call started).

This implies that a temporary can never survive the function in which it was created. More exactly, it cannot survive the scope where it was defined, even if it can in fact survive the full-expression in which it was created.

None of the exceptions in the standard falls out of this restriction, in all cases the lifetime of the temporary is extended to a point that is guaranteed not to exceed the function call in which the temporary was created.

David Rodríguez - dribeas
Thanks to everyone for answering, that was helpful. I am marking this as my preferred answer. Dribeas is correct to point out that precise wording in the standard should not be the issue (nit-picking was not my intention anyway). I support the idea that the chief reason for the distinction is that local references are, well, local to the function, while a class reference member might even be part of an object on the free-store therefore of unknown lifetime, possibly/probably beyond the function call. I can imagine that this defeats any practical stack implementation with proper cleanup.
martin
The fact is that all cases in the standard follow this rule. The fact is that the standard was written by smart people that take feasibility of implementation into account when deciding.
David Rodríguez - dribeas