|
|
|
|
The book was released in 1995 and I have got the final edition 2003. According to this it is waste of time to study the book which are release before 1999? |
I have the same issue with this code: |
|
|
alend wrote: | ||
---|---|---|
|
Framework wrote: |
---|
Constant references accepts literals. Numerical literals don't have an address in memory, so returning a literal is safe... |
const int & i = min (1+2,3+4);
is that temporaries are created and a reference to one of the temporaries is taken.
¿why is that a problem? I think that you can extend the lifetime of temporaries using constant references. |
12.2 Temporary objects ... 4 There are two contexts in which temporaries are destroyed at a different point than the end of the full expression... 5 The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below. A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits. A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call. A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits. In all these cases, the temporaries created during the evaluation of the expression initializing the reference, except the temporary to which the reference is bound, are destroyed at the end of the full-expression in which they are created and in the reverse order of the completion of their construction. |
The book was released in 1995 and I have got the final edition 2003. According to this it is waste of time to study the book which are release before 1999? |
I think it makes sense. Compiler allocates read only memory for const values of 3(1+2) and 7(3+4) first, then passes them to min() |
|
|
const int &t(300);
works too!