Gabriel de Grimouard Gabriel de Grimouard - 1 year ago 60
C++ Question

How const_cast does it job?

I already know what

does. However I would like to know how it does the job. What exactly does the compiler do when it sees a

Answer Source

Recall that const_cast is safe only when the actual variable has been declared non-const. Since all const_cast<T>(...) does is removing const-ness from a type*, it basically does nothing beyond telling the compiler that you know what you are doing. Const-ness is a compile-time concept, so casting it away also happens inside the compiler.

* It can also add/remove const-ness or force it to be treated as if it were volatile, but all of that follows the same basic approach.