From 668993627573221670 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,7458762a2c3b301d X-Google-Attributes: gidf78e5,public X-Google-ArrivalTime: 2001-08-23 14:34: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: jpotter@falcon.lhup.edu (John Potter) Newsgroups: comp.std.c++ Subject: Re: Are literals const? Date: Thu, 23 Aug 2001 21:33:24 GMT Approved: Fergus Henderson , moderator of comp.std.c++ Message-ID: <3b845ae0.41770661@news.csrlink.net> References: <7dc3b1ea.0108220617.421ee131@posting.google.com> X-Trace: mail2news.demon.co.uk 998602409 mail2news:22111 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) X-Newsreader: Forte Free Agent 1.11/32.235 NNTP-Posting-Date: Wed, 22 Aug 2001 21:27:23 EDT Lines: 44 Xref: archiver1.google.com comp.std.c++:7100 On Wed, 22 Aug 2001 20:17:28 GMT, pdimov@mmltd.net (Peter Dimov) wrote: > What do you think about this piece of code: > > int main() > { > int const & r = 5; > const_cast(r) = 6; > } > > Is this legal? I think that it is. Literals aren't const (they are > about 90% const, but not 100% const.) Sure, its "legal", with undefined behaviour. > Similarly: > > int f() > { > return 5; > } > > int main() > { > int const & r = f(); > const_cast(r) = 6; > } Same here. Read the fine print on binding a const& to an rvalue. In the case of built-ins, a temporary is required. The temporary may be of const type and removing const from a const object gives undefined behaviour. The undefined behaviour usually does what you expect because the temp is not really in read only memory. Playing with fire is fun. ;-) John --- [ 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 ]