🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #4096

Quick question about const ref return types

#4096
From: Damian Conway
Date: Thu, 18 Apr 1996 01:53
40 lines
2031 bytes
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                             ]
Technical Details
Message-ID: <199604172311.JAA22065@indy05.cs.monash.edu.au>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back

Show Full Headers
Complete Article Headers
From: Damian Conway <damian@cs.monash.edu.au>
Message-ID: <199604172311.JAA22065@indy05.cs.monash.edu.au>
X-Original-Date: Thu, 18 Apr 1996 09:11:56 +1000 (EST)
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 18 Apr 96 01:53:17 GMT
Approved: fjh@cs.mu.oz.au
Organization: -
Newsgroups: comp.std.c++
Subject: Quick question about const ref return types
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMXWgteEDnX0m9pzZAQEcWQF5AUF8JQaDIiRVy4lDLpnB36dpMXTSyJg8
 wZPHCgLNvDNZXmY9bn83HFoFn3APubYu
 =qVnt