Topic: 12.2/5 T3
Author: Steve Clamage <stephen.clamage@eng.sun.com>
Date: 1997/05/05 Raw View
John E. Potter wrote:
>
> The following non-normative example has caused much confusion
> on clc++m and could use some clarification.
>
> 12.2/5
> class C {
> // ...
> public:
> C();
> C(int);
> friend const C& operator+(const C&, const C&); // problem
> ~C();
> };
> C obj1;
> const C& cr = C(16)+C(23);
> C obj2;
>
> the expression C(16)+C(23) creates three temporaries. ...
>
> ...
> Could someone on the committee state that it is a typo, or ...
It's a typo. In this example, operator+ should be shown as returning
a C by value, not by reference.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1997/05/02 Raw View
The following non-normative example has caused much confusion
on clc++m and could use some clarification.
12.2/5
class C {
// ...
public:
C();
C(int);
friend const C& operator+(const C&, const C&); // problem
~C();
};
C obj1;
const C& cr = C(16)+C(23);
C obj2;
the expression C(16)+C(23) creates three temporaries. A first tempo-
rary T1 to hold the result of the expression C(16), a second temporary
T2 to hold the result of the expression C(23), and a third temporary
T3 to hold the result of the addition of these two expressions. The
temporary T3 is then bound to the reference cr.
Binding the result of the expresion to "C const& cr" is a nice example
of a const reference to a temporary; however, the function does not
return a temporary, it returns a "C const&". With the givens, it is
very difficult (impossible?) to determine where T3 came from. If the
function returns a "C" rather than a "C const&", everything makes sence.
Could someone on the committee state that it is a typo or show us how
T3 came into existence and what its lifetime is?
Thanks,
John
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]