Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const_cast

Reply
Thread Tools

const_cast

 
 
asit
Guest
Posts: n/a
 
      05-04-2011
is there any way to know if const_cast fails ??
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      05-04-2011
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
 
Reply With Quote
 
 
 
 
Martijn van Buul
Guest
Posts: n/a
 
      05-04-2011
* 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Qi
Guest
Posts: n/a
 
      05-05-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const_cast question Gajanan Bhat C++ 9 05-05-2004 12:29 PM
const_cast S.Senthilvel C++ 4 01-08-2004 06:59 PM
const_cast<> R. Anbeeswaran C++ 7 11-14-2003 12:15 PM
const_cast question drowned C++ 3 08-04-2003 08:51 AM
[help] const_cast Kaspar Minosiants C++ 2 07-21-2003 12:43 PM



Advertisments