From 8163565106805701135
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f78e5,7458762a2c3b301d
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2001-08-22 17:15:01 PST
Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!212.74.64.35!colt.net!dispose.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail
From: joerg.barfurth@attglobal.net (Joerg Barfurth)
Newsgroups: comp.std.c++
Subject: Re: Are literals const?
Date: Thu, 23 Aug 2001 00:14:33 GMT
Organization: My beloved Family
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <1eykhdu.1cqeruv1s5f35sN%joerg.barfurth@attglobal.net>
References: <7dc3b1ea.0108220617.421ee131@posting.google.com>
X-Trace: mail2news.demon.co.uk 998525678 mail2news:12614 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)
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
User-Agent: MacSOUP/D-2.4.6 (unregistered for 408 days)
Content-Transfer-Encoding: quoted-printable
X-MIME-Autoconverted: from 8bit to quoted-printable by mulga.cs.mu.OZ.AU id KAA18044
Lines: 55
Xref: archiver1.google.com comp.std.c++:7088

Peter Dimov <pdimov@mmltd.net> wrote:

> What do you think about this piece of code:
>=20
> int main()
> {
>   int const & r =3D 5;
>   const_cast<int &>(r) =3D 6;
> }
>=20
> Is this legal? I think that it is. Literals aren't const (they are
> about 90% const, but not 100% const.)

IMO this is not legal.=20

The literal is not const, but it is an rvalue. (Note: rvalues of builtin
types are never const.) According to 8.5.3/5 (last bullet) a temporary
of type 'const int' is created and the reference is bound to this
temporary.

As the temporary is const (100% :-o), the const_cast + modification
invokes undefined behavior.

> Similarly:
>=20
> int f();

> int main()
> {
>   int const & r =3D f();
>   const_cast<int &>(r) =3D 6;
> }
>=20
> Do we _really_ need non-const literals and rvalues (of built-in
> types)?

I don't think it matters (afaict you can't exploit the non-constness).

BTW: The only literals that are lvalues are string literals (5.1/2).
Those are const to begin with.

Regards, Joerg

--=20
J=F6rg Barfurth                         joerg.barfurth@attglobal.net
<<<<<<<<<<<<< using std::disclaimer;  <<<<<<<<<<<<<<<<<<<<<<<<<<<<
Software Developer                    http://www.OpenOffice.org
StarOffice Configuration              http://www.sun.com/staroffice

---
[ 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                ]



