Topic: in response to "static_cast of mind" by Attila Feher
Author: n.borissov@sympatico.ca ("Nikolai Borissov")
Date: Mon, 9 Sep 2002 17:37:39 +0000 (UTC) Raw View
This is a multi-part message in MIME format.
------=_NextPart_000_06C4_01C25740.EF04FC40
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
May be we should differentiate between non-temporary and temporary
references ? (not to confuse with references to temporary objects).
Temporary reference is one that persists only during expression =
evaluation
time (like temporary variable).
Let me try, first, to be more accurate on definition of cast to =
reference.
Consider a cast to non-constant reference:
static_cast<Widget&>(Some_Object);
The target non-constant reference requires an lvalue to be able to bind =
to
it. That means both standard and user-defined conversions that produce =
rvalues cannot be even
considered. These illegal conversions, in the example above, include =
Widget 1-parameter
non-explicit constructors and Some_Object conversion operators that =
return
rvalues. What is left is conversion operators that return lvalues
(references), and casts from derived to base. So, the rule would sound =
like
this:
| Conversion to reference either accepts an exact lvalue or consists of =
operand's
| user-defined conversion returning lvalue of target or derived object =
and/or
| (following) reference cast from derived to target object.
This rule agrees with the intuitive understanding of a reference.
Now the question: should this rule apply to a cast to reference-to-const =
too?
Here the difference between non-temporary and temporary references may =
come
into play if we do apply this rule, but only to temporary references. I =
know
only 2 types of temporary references, those resulted from a cast and
returned by a function. So, with the rule in effect static_cast<const
Widget&>(x) must behave similar to static_cast<Widget&>(x). The only
difference is that cast to const may swallow a temporary argument too:
static_cast<Widget&>(Widget(3)); // illegal, cannot bind to temporary
static_cast<const Widget&>(Widget(3)); // OK
Non-temporary references to constant objects, i.e. those explicitly =
declared
in the program, follow the extended rule that allows user-defined
conversions returning rvalues. I think that such references are similar =
to
references-parameters and they use the same mechanizm of allocating =
rvalues
in the stack. Apparently, this extended rule was originally designed to
allow parameter conversions and then was applied to explicitly declared
references to constants.
Now about the standard (5.2.9/2). It says about "invented" "temporary"
variable. That means referring to declaration "T t(a)" of non-temporary
reference is incorrect. Another words, we have a situation here where =
rules
of well-formity are different for temporary and non-temporary references =
to const objects. The
well-formed expression "T t(a)" for a non-temporary reference-to-const =
may
be ill-formed for temporary one.
This is my wild guess. I still don't have standard and cannot verify it
myself. Even if this idea is wrong, I hope it'll shed some light to the =
problem.
Nikolai Borissov
------=_NextPart_000_06C4_01C25740.EF04FC40
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2719.2200" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Times New Roman" =
size=3D3>May be we=20
should differentiate between non-temporary and temporary<BR>references =
? =20
(not to confuse with references to temporary objects).<BR>Temporary =
reference is=20
one that persists only during expression evaluation<BR>time (like =
temporary=20
variable).<BR><BR>Let me try, first, to be more accurate on definition =
of cast=20
to reference.<BR>Consider a cast to non-constant=20
reference:<BR></FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Times New Roman"=20
size=3D3>static_cast<Widget&>(Some_Object);<BR></DIV></FONT></F=
ONT>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Times New Roman" =
size=3D3>The target=20
non-constant reference requires an lvalue to be able to bind to<BR>it. =
That=20
means both standard and user-defined conversions that produce rvalues =
cannot be=20
even<BR>considered. These illegal conversions, in the example=20
above, include Widget 1-parameter<BR>non-explicit constructors and=20
Some_Object conversion operators that return<BR>rvalues. What is left is =
conversion operators that return lvalues<BR>(references), and casts from =
derived=20
to base. So, the rule would sound like<BR>this:<BR></FONT></FONT><FONT=20
face=3DArial size=3D2><FONT face=3D"Times New Roman" size=3D3>| =
Conversion to reference=20
either accepts an exact lvalue or consists of =
operand's</FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Times New Roman" =
size=3D3>|=20
user-defined conversion returning lvalue of target or derived =
object=20
and/or</FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Times New Roman" =
size=3D3>| (following)=20
reference cast from derived to target object.<BR><BR>This rule agrees =
with the=20
intuitive understanding of a reference.<BR>Now the question: should this =
rule=20
apply to a cast to reference-to-const too?<BR>Here the difference =
between=20
non-temporary and temporary references may come<BR>into play if we do =
apply this=20
rule, but only to temporary references. I know<BR>only 2 types of =
temporary=20
references, those resulted from a cast and<BR>returned by a function. =
So, with=20
the rule in effect static_cast<const<BR>Widget&>(x) must =
behave=20
similar to static_cast<Widget&>(x). The only<BR>difference is =
that=20
cast to const may swallow a temporary argument=20
too:<BR><BR>static_cast<Widget&>(Widget(3)); // illegal, =
cannot bind=20
to temporary<BR>static_cast<const Widget&>(Widget(3)); //=20
OK<BR><BR>Non-temporary references to constant objects, i.e. those =
explicitly=20
declared<BR>in the program, follow the extended rule that allows=20
user-defined<BR>conversions returning rvalues. I think that such =
references are=20
similar to<BR>references-parameters and they use the same mechanizm of=20
allocating rvalues<BR>in the stack. Apparently, this extended rule was=20
originally designed to<BR>allow parameter conversions and then was =
applied to=20
explicitly declared<BR>references to constants.<BR><BR>Now about the =
standard=20
(5.2.9/2). It says about "invented" "temporary"<BR>variable. That means=20
referring to declaration "T t(a)" of non-temporary<BR>reference is =
incorrect.=20
Another words, we have a situation here where rules<BR>of well-formity =
are=20
different for temporary and non-temporary references to const objects.=20
The<BR>well-formed expression "T t(a)" for a non-temporary =
reference-to-const=20
may<BR>be ill-formed for temporary one.<BR><BR>This is my wild guess. I =
still=20
don't have standard and cannot verify it<BR>myself. Even if this idea is =
wrong,=20
I hope it'll shed some light to the problem.<BR><BR>Nikolai=20
Borissov</FONT><BR></DIV></FONT></BODY></HTML>
------=_NextPart_000_06C4_01C25740.EF04FC40--
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]