8

Is it true that const_cast is just a way to tell the compiler "stop moaning, treat this as a non-const pointer"? Are there any cases when const_cast itself is translated into actual machine code?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
sharptooth
  • 167,383
  • 100
  • 513
  • 979

4 Answers4

12

No, it just removes const attribute at compile time.

Evgeny Lazin
  • 9,193
  • 6
  • 47
  • 83
5

Conceivably,there could be architectures where a const pointer had a different representation to a non-const one, in which case the compiler would have to emit some code. I'm not aware of any such architectures, however.

  • Clever, but not true: const is attribute only, it does not mean you cannot write (consider mutable members). If you are conceiving an architecture where read-only and read-write or write-only pointers are different, then the code still will not be generated on cast, rather on the write itself. – Suma Apr 17 '09 at 07:47
  • 1
    I intentionally didn't say _when_ the compiler would have to emit some code. –  Apr 17 '09 at 08:08
  • 1
    Doesn't matter at all. The const_cast woud remove the hypothetical "read-only bit" in the pointer, as would writes to a mutable member through a const pointer. The latter can be detected with 100% acuracy by the compiler. – MSalters Apr 17 '09 at 13:39
  • Yeah, if anything, then would the conversion need to unset/set the bit. Writes to mutable members don't concern the pointer to the class-type anymore, since at that time, it's already de-referenced. Also note that what can be different at most are the bits that don't participate in the value calculation of the pointer. Those bits that do (value-representation) must be identical for T* and T const* (see 3.9.2/3). – Johannes Schaub - litb Jun 01 '09 at 16:53
  • "_there could be architectures where a const pointer had a different representation to a non-const one_" No. – curiousguy Jul 20 '12 at 13:52
  • @MSalters "_The const_cast woud remove the hypothetical "read-only bit" in the pointer_" This bit cannot exist, period. – curiousguy Jul 20 '12 at 13:53
  • 1
    @curiousguy: That statement is exactly what is questioned here. If you have proof, please add that as an answer. – MSalters Jul 20 '12 at 14:15
3

const_cast just throws away the constness of an attribute and nothing more.

Shree
  • 4,627
  • 6
  • 37
  • 49
2

const does not change the representation of a type, in particular, the representation of T* is exactly the same as T const*.

Consider:

int i, 
    * const cpi = &i,
    * const * pcpi = &cpi;
int const * const * pcpci = pcpi; // cpi is now int const*

The representation of cpi at the same time represents an int* and a int const* via *pcpci.

There is no way for the representation of cpi to change when pcpci is initialised.

Of course, if the pointer objects have non-significant bits, the compiler can flip them randomly; in particular, const_cast can flip the non-significant bits of a pointer, but any implicit conversion could also. I don't think this case exists in the real world.

The same apply when two different bit patterns result in the same address value (base+offset when offset is big enough).

curiousguy
  • 8,038
  • 2
  • 40
  • 58