Topic: Is static_cast<int>(const char&) legal?
Author: "Matt Seitz" <mseitz@meridian-data.com>
Date: 1998/10/16 Raw View
saroj@bear.com wrote in message <702oi5$oqq$1@nnrp1.dejanews.com>...
>My Sun compiler complains that the static_cast from const char& to int
>is not allowed. But the C style old cast works.
>What does the standard say?
My guess is that the compiler is interpreting the "static_cast<int>" as an
attempt to cast away the "const" part of "const char &". I would try:
return static_cast<const int>(c);
[ 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: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/10/17 Raw View
Florian Weimer wrote:
>
> According to the standard, you cannot cast away constness using
> static_cast<>. You have to use const_cast<> instead.
But the statement is copying a value. It doesn't need to preserve
const-ness. const_cast<> is only needed for changing other than
top-level const-ness. If the left-hand side was also a reference,
however, the cast wouldn't work, because it would be in effect a pointer
assignment, and the const-ness would be one level of indirection down.
--
Ciao,
Paul
---
[ 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: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/10/17 Raw View
Matt Seitz<mseitz@meridian-data.com> wrote:
>saroj@bear.com wrote in message <702oi5$oqq$1@nnrp1.dejanews.com>...
>>My Sun compiler complains that the static_cast from const char& to int
>>is not allowed. But the C style old cast works.
>>What does the standard say?
>
>My guess is that the compiler is interpreting the "static_cast<int>" as an
>attempt to cast away the "const" part of "const char &". I would try:
>
>return static_cast<const int>(c);
We're dealing with a compiler bug here. You might as well just say
(int)c
and be done with it.
--
Nathan Myers
ncm@nospam.cantrip.org http://www.cantrip.org/
---
[ 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: saroj@bear.com
Date: 1998/10/14 Raw View
int foo(const char& c)
{
return static_cast<int>(c);
}
My Sun compiler complains that the static_cast from const char& to int
is not allowed. But the C style old cast works.
What does the standard say?
Thank you,
Saroj Mahapatra
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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: "Jim Barry" <jim.barry@bigfoot.com>
Date: 1998/10/15 Raw View
saroj@bear.com wrote in message <702oi5$oqq$1@nnrp1.dejanews.com>...
>
>int foo(const char& c)
>{
> return static_cast<int>(c);
>}
>
>My Sun compiler complains that the static_cast from const char& to int
>is not allowed. But the C style old cast works.
Looks alright to me. It compiles on MSVC5.
- Jim
--
Jim Barry, Thermoteknix Systems Ltd., Cambridge, UK.
http://www.thermoteknix.co.uk - Queen's Award for Export 1998
http://www.geocities.com/SiliconValley/2060
[ 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: Florian Weimer <fw_u_comp.std.c++_m3lnmi6j0q@cygnus.stuttgart.netsurf.de>
Date: 1998/10/16 Raw View
saroj@bear.com writes:
> My Sun compiler complains that the static_cast from const char& to int
> is not allowed. But the C style old cast works.
> What does the standard say?
According to the standard, you cannot cast away constness using
static_cast<>. You have to use const_cast<> instead.
---
[ 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: Ron Natalie <ron@sensor.com>
Date: 1998/10/16 Raw View
saroj@bear.com wrote:
>
> int foo(const char& c)
> {
> return static_cast<int>(c);
> }
>
> My Sun compiler complains that the static_cast from const char& to int
> is not allowed. But the C style old cast works.
> What does the standard say?
The standard says the Sun compiler is wrong. The spec says that
you can static_cast something provided (DAMNED ANSI DOC WEENIES
WHO WON'T LET YOU CUT FROM THE PDF FILE) "if the declaration
T t(e);" is well-formed for some invented variable t.
In your case e is (c), T is int. It's clearly well-formed
to initialize an int from a const char. As a matter of fact,
the sun eats that code fine:
int foo(const char& c) {
int t(c);
return t;
}
The Sun SparcPro compiler even at 4.2 is pretty divergent
from the C++ standard.
---
[ 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: Greg Lanthier <greg@nexus.carleton.ca>
Date: 1998/10/16 Raw View
I'm not an authority on this matter, but I have read somewhere (Probably
in one of the Meyers books) that in order to cast away constness you
should use the const_cast<...> operator.
This const_cast<...> facility will only cast away the volatileness of the
paramater. Doing something like this won't (shouldn't) work:
const char c = 'G';
main() {
int i = const_cast<int> c; // const_cast<...> shouldn't be used to
// changed the type of an attribute.
cout << i << endl;
}
You could try something like this, though....
int foo( const char& c )
{
return static_cast<int>( const_cast<char> c );
}
This might do what your looking for. Read more about thie C++ casting
operators in the ''More Effective C++'' book by Scott Meyers. His books
are the most useful references I have found for C++ outside of the ANSI
C++ standard.
saroj@bear.com wrote:
: int foo(const char& c)
: {
: return static_cast<int>(c);
: }
---
[ 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 ]