0

I am experimenting with const_cast operator and trying to override a const status of the parameter o passed as an argument:

void function1 (const Object *o)
{
    ...
    function2( const_cast < Object *> ( o ) ); //Exception in g++
}

void function2 (Object *o) {}

But overriding a const status of o throws an exception in g++ (GNU/Linux), in VS 2010 (Win) it works well ...

Is there any more reliable way how to override a const status of a function parameter?

Update:

MSDN write: You cannot use the const_cast operator to directly override a constant variable's constant status :-(.

curiousguy
  • 8,038
  • 2
  • 40
  • 58
justik
  • 4,145
  • 6
  • 32
  • 53
  • 5
    What exception are you getting? The code you posted is fine in g++. – Daniel Jul 20 '12 at 13:20
  • Please show us where you call `function1()`, and keep in mind that using `const_cast` to remove constness from a truly-const object (a `const Object` in this case) results in undefined behavior, up to and including exceptions. – cdhowie Jul 20 '12 at 13:22
  • 3
    Can you make the code you show a *complete* (but still minimal) example? It's hard to understand what you're seeing from this fragment. – Flexo Jul 20 '12 at 13:23
  • 2
    @cdhowie: only if someone uses the resulting pointer-to-non-const to attempt to modify the object. `const int i = 0; int *p = const_cast(&i); std::cout << *p;` is OK. – Steve Jessop Jul 20 '12 at 13:26
  • @cdhowie. I am afraid, that this exception represents an undefined behavior :-(. This code is only illustration, the real code with exceptions is a part of an extensive library... – justik Jul 20 '12 at 13:27
  • 2
    `const_cast` isn't magic, and almost all of its naive CV-removing uses are in fact undefined behaviour. It's only intended for something like `strchr` where you reuse one piece of code for two different constnesses. – Kerrek SB Jul 20 '12 at 13:27
  • @SteveJessop Of course, but I really doubt that this example reflects the OP's actual code. The odds here are that the object pointed to really is const. – cdhowie Jul 20 '12 at 13:58
  • @cdhowie: I'm not talking about whether the object is const (although if it were non-const, that also would prevent there being any UB). I'm talking about whether the pointer-to-non-const is used to modify the object, or only to read it. The latter would happen for example if the reason you const-cast is in order to work around an incorrect third-party interface, which does not modify the object but isn't marked const. – Steve Jessop Jul 20 '12 at 14:16

1 Answers1

3

MSDN write: You cannot use the const_cast operator to directly override a constant variable's constant status :-(.

const_cast allows you to strip const specifier from the pointer, but it does not affect the "const status" of the value itself. Compiler might decide to put the value into read-only memory (well, it's const!) and then an attempt to modify it, even through const_cast, might result in access violation.

Here is a code snippet:

static const int A = 1; // it's constant, it might appear on a read-only memory page
static int B = 2; // it's a regular variable

const int* pA = &A; // a const pointer
int* pB1 = &B; // a pointer
const int* pB2 = &B; // a const pointer to regular variable

*pA = 0; // Compiler error, we are not allowed to modify const
*const_cast<int*>(pA) = 1; // Runtime error, although const specifier is stripped from the variable, it is still on read-only memory and is not available for updates
*pB1 = 2; // OK
*pB2 = 3; // Compiler error, we are not allowed to modify const
*const_cast<int*>(pB2) = 4; // OK, we stripped const specifier and unlocked update, and the value is availalbe for update too because it is a regular variable

That is, const_cast removes const specifier during compile time, however it is not its purpose, authority or design to alter access mode of the underlying memory.

Roman R.
  • 68,205
  • 6
  • 94
  • 158