From 4830448243493046869
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,7458762a2c3b301d
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2001-08-23 10:49:01 PST
Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!dispose.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail
From: thp@cs.ucr.edu
Newsgroups: comp.std.c++
Subject: Re: Are literals const?
Date: Thu, 23 Aug 2001 17:48:17 GMT
Organization: University of California, Riverside
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <9m24k7$e2d$1@glue.ucr.edu>
References: <7dc3b1ea.0108220617.421ee131@posting.google.com>
X-Trace: mail2news.demon.co.uk 998588904 mail2news:20158 mail2news mail2news.demon.co.uk
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
NNTP-Posting-Date: Thu, 23 Aug 2001 05:33:59 +0000 (UTC)
User-Agent: tin/pre-1.4-19990517 ("Psychonaut") (UNIX) (Linux/2.4.2-XFS (i686))
Lines: 67
Xref: archiver1.google.com comp.std.c++:7093

Peter Dimov <pdimov@mmltd.net> wrote:
: What do you think about this piece of code:

: int main()
: {
:   int const & r = 5;
:   const_cast<int &>(r) = 6;
: }

: Is this legal? I think that it is. Literals aren't const (they are
: about 90% const, but not 100% const.)

: Similarly:

: int f()
: {
:   return 5;
: }

: int main()
: {
:   int const & r = f();
:   const_cast<int &>(r) = 6;
: }

AFIK, they are both legal, and so is:

   int main()
   {
     int x = 5;
     int const & r = x;
     const_cast<int &>(r) = 6;
   }

IIRC, a const reference can refer to a non-const object but can't be
used to modify it.

: Do we _really_ need non-const literals and rvalues (of built-in
: types)?

Until recently I though that only lvalues could be const, but rvalues
of struct/class types have a this-pointer by which their member
functions can modify them.  So:

struct Stuff {
 void bar() const;
 void baz();
};

const Stuff foo();

int main() {
  foo().bar(); // cool
  foo().baz(); // error -- violates constness of rvalue foo().
}

That wedge opens the door for both const and volatile rvalues of all
types.  (I'm still getting used to the notion.)

Tom Payne

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]



