const_cast

N

Noah Roberts

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.
 
M

Martijn van Buul

* 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.
 
Q

Qi

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top