From -4905263377698427761
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f78e5,7458762a2c3b301d
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2001-08-22 17:02:01 PST
Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news.uchicago.edu!vixen.cso.uiuc.edu!howland.erols.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:01:23 GMT
Organization: My beloved Family
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <1eykhqq.1kjapbbtv7igeN%joerg.barfurth@attglobal.net>
References: <7dc3b1ea.0108220617.421ee131@posting.google.com> <memo.20010822222216.55863D@brangdon.madasafish.com>
X-Trace: mail2news.demon.co.uk 998524888 mail2news:12489 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 KAA17229
Lines: 89
Xref: archiver1.google.com comp.std.c++:7087

Dave Harris <brangdon@cix.co.uk> wrote:

> pdimov@mmltd.net (Peter Dimov) wrote (abridged):
> > 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.
>=20
> I think it is too. The first line is, the second line is and I don't se=
e
> any undefined behaviour. The type of 5 is (int), not (const int).
>=20
> > Literals aren't const (they are about 90% const, but not 100% const.)
>=20
> I think the reference is bound to a temporary, not a literal.=20
> However, I don't know when and how it stops being a literal.

As I argued in another post: This is described in 8.5.3/5 (last bullet).
The temporary has type 'const int' and thus the code is not legal.

> > Do we _really_ need non-const literals and rvalues (of built-in
> > types)?
>=20
> Yes. I'd prefer to minimise the differences between built-in types and
> class types.=20

The present system is pretty close at that (in this regard).

> Eg:
>=20
>     class Complex {
>     public:
>          Complex( int i, int j=3D0 );
>          //...
>     };
>=20
>     Complex const &r =3D 1;
>     const_cast<Complex &>(r) =3D 2;
>=20
> This is more or less reasonable for class types, [...]

But it is illegal as well. The same bullet of 8.5.3/5 applies.

Now, if you had=20

    Complex const & r =3D Complex(1);

the initializer is an rvalue of type Complex (non-const).=20

In this case the next-to-last paragraph of 8.5.3/5 applies. This allows
binding the reference to that rvalue directly. But it also allows
copying to another temporary,which would have type 'const Complex'.

This means that the code for most means is still illegal. I wonder
though, why the choice is 'implementation-defined' here rather than
'unspecified'. This means, that if an implementation documents that it
always uses the direct way of binding here, the code would be legal for
that implementation.

BTW: What choice is documented (or is it undocumented ?) for existing
compilers ?=20
(Sorry, I have no compiler here, so I can't start the list).

> [...] so it should be permitted=20
> for built-in types too, for the sake of templates:

> Unless there is a strong reason to have a difference. Is there?

Which now means it shouldn't ...

Regards, J=F6rg

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



