Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   const_cast (http://www.velocityreviews.com/forums/t747761-const_cast.html)

asit 05-04-2011 06:40 PM

const_cast
 
is there any way to know if const_cast fails ??

Noah Roberts 05-04-2011 06:55 PM

Re: const_cast
 
On 5/4/2011 11:40 AM, asit wrote:
> is there any way to know if const_cast fails ??


It won't compile. There's actually no such thing as failing to remove
constness. No, there's no way to check if doing so will turn the world
inside out.

--
http://crazycpp.wordpress.com

Martijn van Buul 05-04-2011 07:06 PM

Re: const_cast
 
* asit:
> is there any way to know if const_cast fails ??


const_cast can't "fail", other than with a compiler error if you're
casting away more than just 'const'. However, this doesn't mean that
the resulting code will actually work. It may still trigger undefined
behaviour, and there's no way to "detect" that at runtime.

In that respect, const_cast *always* fails; you're throwing away a
valuable contract, and is inherently dangerous. I suppose it is still
preferable over a C-style cast, though.

--
Martijn van Buul - pino@dohd.org

Qi 05-05-2011 02:05 AM

Re: const_cast
 
On 2011-5-5 2:40, asit wrote:
> is there any way to know if const_cast fails ??


Others had answered quite well.

What I want to say is that const_cast is a compile time operator, not
runtime operator.

That means, if it "fails", the code won't get compiled.
If the code is compiled, the operator won't fail any more at runtime.


--
WQ


All times are GMT. The time now is 04:42 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.