Article View: comp.std.c++
Article #3901Re: Rationale behind disallowal of non-const r
From: Ross Smith
Date: Wed, 03 Apr 1996 08:04
Date: Wed, 03 Apr 1996 08:04
49 lines
2916 bytes
2916 bytes
Steve Clamage wrote:
>
> Yes, there are times when you would like to be able to bind an rvalue
> to a non-const reference, since the only things you care about are
> sure to happen anyway. For example, given a class T:
> T f1(); // f1 returns a T rvalue
> f1().foo(); // ok only if foo is a const member function of T
I find this very surprising; perhaps I'm misunderstanding you. This seems
to disallow some very useful techniques.
As an example, suppose I have a string class with some member functions:
class String {
public:
String Substring(int start, int length) const;
// Extract a substring
const String& LowerCase();
// Convert to lower case, and return a const reference to *this
};
Are you saying I can't write something like:
String foo, bar;
foo = bar.Substring(1, 10).LowerCase();
(i.e. the call to Substring() returns a temporary, which is then converted
to lower case, and copied to foo via String::operator= before the temp is
destroyed -- at least, that's what I thought would happen)
I use this sort of technique frequently, and neither of the compilers I use
(Symantec C++ 7.21 and IBM Visual Age C++ 3.0) raise any objection (IBM's
class library explicitly encourages this sort of thing, in fact).
Have I (and IBM and Symantec) got it wrong, or have I just misunderstood
what you wrote? If my code *is* legal, could you explain how it differs
from yours, which seems to be doing exactly the same thing?
--
Ross Smith ........................................ Wellington, New Zealand
Home: <mailto:alien@netlink.co.nz> ... Work: <mailto:ross.smith@nz.eds.com>
"I keep my ear very close to the ground, and in consequence I listen to a
lot of dog crap." -- Alexei Sayle
---
[ 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:
<31622189.3172@netlink.co.nz>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<DAVEW.96Mar27195129@trigati.cs.haverford.edu> <4jevb1$kkp@engnews1.Eng.Sun.COM>Show Full Headers
Complete Article Headers
From: Ross Smith <alien@netlink.co.nz> Message-ID: <31622189.3172@netlink.co.nz> X-Original-Date: Wed, 03 Apr 1996 18:58:17 +1200 Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 03 Apr 96 08:04:22 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Re: Rationale behind disallowal of non-const r Organization: Department of High Energy Magic, Unseen University References: <DAVEW.96Mar27195129@trigati.cs.haverford.edu> <4jevb1$kkp@engnews1.Eng.Sun.COM> X-Mailer: Mozilla 2.01 (Win95; I) X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMWIyDuEDnX0m9pzZAQELIAF9FehCOLXcU5gJtUnIluwaop7Q1Pi9TxzU pJyi+LkDYxvujG8+u7MqX1Tx+ZftPwFX =8CUw