Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #4690 > unrolled thread

const_cast

Started byasit <lipun4u@gmail.com>
First post2011-05-04 11:40 -0700
Last post2011-05-05 10:05 +0800
Articles 4 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  const_cast asit <lipun4u@gmail.com> - 2011-05-04 11:40 -0700
    Re: const_cast Noah Roberts <dont@email.me> - 2011-05-04 11:55 -0700
    Re: const_cast Martijn van Buul <pino@dohd.org> - 2011-05-04 19:06 +0000
    Re: const_cast Qi <no@no.no> - 2011-05-05 10:05 +0800

#4690 — const_cast

Fromasit <lipun4u@gmail.com>
Date2011-05-04 11:40 -0700
Subjectconst_cast
Message-ID<f044ae2a-88f0-49c2-9f09-619cad881832@glegroupsg2000goo.googlegroups.com>
is there any way to know if const_cast fails ??

[toc] | [next] | [standalone]


#4692

FromNoah Roberts <dont@email.me>
Date2011-05-04 11:55 -0700
Message-ID<4dc1a0fa$0$2413$cc2e38e6@news.uslec.net>
In reply to#4690
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

[toc] | [prev] | [next] | [standalone]


#4693

FromMartijn van Buul <pino@dohd.org>
Date2011-05-04 19:06 +0000
Message-ID<slrnis38ui.1ged.pino@mud.stack.nl>
In reply to#4690
* 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 

[toc] | [prev] | [next] | [standalone]


#4701

FromQi <no@no.no>
Date2011-05-05 10:05 +0800
Message-ID<ipt0m6$msm$1@speranza.aioe.org>
In reply to#4690
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web