"Toon Knapen" wrote in message
news:200302181202.36464.toon.knapen@si-lab.org...
| On Monday 17 February 2003 19:21, Ivan Vecerina wrote:
| > matrix<double> const& r2 = foo(); // always optimized
| > // but can trigger a VC7 bug
|
| Here you're taking a const-reference to a temporary.
| From the moment you're using r2 you have 'undefined behaviour' IMHO.
Not at all !
When a temporary is bound to a const-reference, its lifetime is
extended to the lifetime of that reference.
This is clearly specified in the C++ standard at 12.2/5.
For a discussion of this feature (and beyond) by the C++ working
group back in 1993, see:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/1993/N0345.pdf
This had already been decided in 1993,
VC6 supports the behavior correctly. Unfortunately, in recent trials
I made with VC7, it seems that support for this C++ feature is
broken (in some complex functions, premature re-use of the memory
allocated to the temporary seemed to occur).
This feature is useful to help avoid unnecessary copies of the
value returned by an expression. It also allows the safe
conversion of functions returning a const reference to a
function returning a temporary (if needed during refactoring).
Best regards,
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Soft Dev Manger, xitact <> http://www.xitact.com
Brainbench MVP for C++ <> http://www.brainbench.com