RE: [boost] Re: static_cast and const

Jonathan Turkanis <technews@kangaroologic.com> wrote:
"David Abrahams" <dave@boost-consulting.com> wrote in message news:umzzl3pvk.fsf@boost-consulting.com...
Richard Hadsell <hadsell@blueskystudios.com> writes:
Jonathan Turkanis wrote:
When I was writing it I initially typed 'const_cast', but then thought to myself: why use my big guns if I don't have to? ;-)
For clarity -- to tell the reader that you intended to modify the const-ness.
static_cast is a bigger gun than const_cast. At least const_cast cant violate data layout, and unless there is actually a constant object, won't result in undefined behavior.
Good point. I guess part of the reason I avoid const_cast whenever possible is that people look upon it suspiciously, even if it's only adding cv-qualification. Maybe people should be more suspicious of static_cast.
If you want to make an implicit conversion explicit, use boost::implicit_cast.

"Ben Hutchings" <ben.hutchings@businesswebsoftware.com> writes:
Jonathan Turkanis <technews@kangaroologic.com> wrote:
"David Abrahams" <dave@boost-consulting.com> wrote in message news:umzzl3pvk.fsf@boost-consulting.com...
Richard Hadsell <hadsell@blueskystudios.com> writes:
Jonathan Turkanis wrote:
When I was writing it I initially typed 'const_cast', but then thought to myself: why use my big guns if I don't have to? ;-)
For clarity -- to tell the reader that you intended to modify the const-ness.
static_cast is a bigger gun than const_cast. At least const_cast cant violate data layout, and unless there is actually a constant object, won't result in undefined behavior.
Good point. I guess part of the reason I avoid const_cast whenever possible is that people look upon it suspiciously, even if it's only adding cv-qualification. Maybe people should be more suspicious of static_cast.
If you want to make an implicit conversion explicit, use boost::implicit_cast.
That's the right answer. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

"David Abrahams" <dave@boost-consulting.com> wrote
"Ben Hutchings" <ben.hutchings@businesswebsoftware.com> writes:
If you want to make an implicit conversion explicit, use boost::implicit_cast.
That's the right answer.
Would it be an idea to put a reference to this together with the other conversion functions in your docs, eg Boost Conversion Library? Its the first I've heard of it. regards Andy Little

"Andy Little" <andy@servocomm.freeserve.co.uk> writes:
"David Abrahams" <dave@boost-consulting.com> wrote
"Ben Hutchings" <ben.hutchings@businesswebsoftware.com> writes:
If you want to make an implicit conversion explicit, use boost::implicit_cast.
That's the right answer.
Would it be an idea to put a reference to this together with the other conversion functions in your docs, eg Boost Conversion Library? Its the first I've heard of it.
Yes, it would be "an idea" ;-) If someone will write the docs, they can be added. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
participants (3)
-
Andy Little
-
Ben Hutchings
-
David Abrahams