Topic: Can static_cast cast away constness from void*?


Author: eaux@home.com
Date: 1999/07/13
Raw View
I'm in a bit of a dispute with a compiler vendor about the behavior of
static_cast, and want to see if I can get an authoritative answer from the
C++ oracle :-). I'm sure something as pedestrian as this has already been
nailed down, but since my searches have so far been fruitless, here
goes...The question is: can I cast a (const void*) to (Foo*) using
static_cast?I had thought the answer was &quotno&quot after reading C++ PL
3rd ed., 15.4.2.1. However, although one compiler I use flags an error when
trying to do this, another compiler (unfortunately for me!) lets me get away
with this.When I reported this as a bug to the latter compiler vendor, their
reply merely cast various aspersions upon my ken of C++. So, I went digging
through the spec. ISO/IEC 14882:1998, section 5.2.9.1 (pg. 72),
states:&quotThe static_cast operator shall not cast away constness.&quotThat
seems clear enough. However, section 5.2.9.10 says this:&quotAn rvalue of
type 'pointer to cv void' can be explicitly converted to a pointer to object
type.&quotHere, the spec makes no explicit mention of what this &quotpointer
to object type&quot is allowed to be. This seems to imply that any
cv-qualification of the result type is allowed, and therefore, that the
cv-qualification of void* can be ignored.Is this correct? Can you clarify
this for me?Thanks,Owen

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: wmm@fastdial.net
Date: 1999/07/13
Raw View
In article <7me2nq$461$1@nnrp1.deja.com>,
  eaux@home.com wrote:
>  I went digging
> through the spec. ISO/IEC 14882:1998, section 5.2.9.1 (pg. 72),
> states:&quotThe static_cast operator shall not cast away
constness.&quotThat
> seems clear enough. However, section 5.2.9.10 says this:&quotAn rvalue
of
> type 'pointer to cv void' can be explicitly converted to a pointer to
object
> type.&quotHere, the spec makes no explicit mention of what this
&quotpointer
> to object type&quot is allowed to be. This seems to imply that any
> cv-qualification of the result type is allowed, and therefore, that
the
> cv-qualification of void* can be ignored.Is this correct?

The statements in 5.2.9p1 are general requirements on the behavior
of static_cast; all the various varieties of static_cast must obey
the restrictions of 5.2.9p1, including the prohibition of casting
away constness.

The wording in 5.2.9p10 is unfortunate, especially when compared
with, e.g., p5, p8, and p9 where the treatment of cv-qualification
is explicit, leading to the impression that something different was
intended in p10.  However, that's not the case.  In the absence of
a specific exception, the general rule from p1 still applies.

--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: horst.kraemer@snafu.de (Horst Kraemer)
Date: 1999/07/13
Raw View
On 13 Jul 99 02:32:10 GMT, eaux@home.com wrote:

> .......The question is: can I cast a (const void*) to (Foo*) using
> static_cast?

No.

> ...So, I went digging
> through the spec. ISO/IEC 14882:1998, section 5.2.9.1 (pg. 72),
> states:&quotThe static_cast operator shall not cast away constness.&quotThat
> seems clear enough. However, section 5.2.9.10 says this:&quotAn rvalue of
> type 'pointer to cv void' can be explicitly converted to a pointer to object
> type.&quotHere, the spec makes no explicit mention of what this &quotpointer
> to object type&quot is allowed to be. This seems to imply that any
> cv-qualification of the result type is allowed, and therefore, that the
> cv-qualification of void* can be ignored.Is this correct?


I don't think so. It's a simple exercise in reading:

5.2.9.10 is a subparagraph of 5.2.9.6 which says that inverses of
standard conversions by static_cast are allowed (with some exceptions)
provided that they don't cast away constness "and the following
additional rules for specific cases". The additional rules are 5.2.9.7
to 5.2.9.10.

Thus

 const void * p;

 int * q = static_cast<int*>(p);


is clearly disallowed - under the condition that this cast fulfills
the definition of "casting away constness".


Regards
Horst
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Michael S. Scherotter" <mss@tartus.com>
Date: 1999/07/13
Raw View
try const_cast
that is what it is meant for.


eaux@home.com wrote:

> I'm in a bit of a dispute with a compiler vendor about the behavior of
> static_cast, and want to see if I can get an authoritative answer from the
> C++ oracle :-). I'm sure something as pedestrian as this has already been
> nailed down, but since my searches have so far been fruitless, here
> goes...The question is: can I cast a (const void*) to (Foo*) using
> static_cast?I had thought the answer was &quotno&quot after reading C++ PL
> 3rd ed., 15.4.2.1. However, although one compiler I use flags an error when
> trying to do this, another compiler (unfortunately for me!) lets me get away
> with this.When I reported this as a bug to the latter compiler vendor, their
> reply merely cast various aspersions upon my ken of C++. So, I went digging
> through the spec. ISO/IEC 14882:1998, section 5.2.9.1 (pg. 72),
> states:&quotThe static_cast operator shall not cast away constness.&quotThat
> seems clear enough. However, section 5.2.9.10 says this:&quotAn rvalue of
> type 'pointer to cv void' can be explicitly converted to a pointer to object
> type.&quotHere, the spec makes no explicit mention of what this &quotpointer
> to object type&quot is allowed to be. This seems to imply that any
> cv-qualification of the result type is allowed, and therefore, that the
> cv-qualification of void* can be ignored.Is this correct? Can you clarify
> this for me?Thanks,Owen

--
Michael S. Scherotter           |Architectural Design Tools
________________________________|AutoCAD Applications
Lead Software Developer         |Custom CAD Solutions
Tartus Development, Inc.        |OpenGL & Open Inventor
630 Las Gallinas Ave. Suite 300 |MFC, OLE, COM, & ATL
San Rafael, CA 94903            |OLE for Design and Modeling
                                |IAI IFC
                                |___________________________
(415) 491-8920 x 12                    mailto:mss@tartus.com
(415) 491-8921 (fax)                   http://www.tartus.com




[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]