Topic: Quick question about const ref return types


Author: Damian Conway <damian@cs.monash.edu.au>
Date: 1996/04/18
Raw View
Is the following legal?

 const int& positive(const int& value)
 {
  if (value<0)
  {
   return 0;
  }
  else
  {
   return value;
  }
 }

If so, I presume it's also valid in the general case:

 const ClassType& func(const ClassType& value);

and that the return of a literal generates a temporary which is bound to
the reference returned and persists until the completion of the full
expression enclosing the function call (or the destruction of a reference
variable to which the returned reference is subsequently bound).

I've searched through the April 95 WP for a definitive answer.
8.5.3. [dcl.init.ref] seems to imply this usage is valid (by
analogy to the binding of temporaries to const reference parameters).

damian
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  who: Damian Conway               email: damian@bruce.cs.monash.edu.au
where: Dept. Computer Science      phone: +61-3-565-5184
       Monash University             fax: +61-3-565-5146
       Clayton 3168                quote: "A pessimist is never disappointed."
       AUSTRALIA
---
[ 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                             ]