const_cast in c++  is used to cast away the constness and volatility of a variables. const_cast is used to remove the const-ness from references and pointers that ultimately refer to something that is not const. so it not allowed to cast away variables those are originally constant. Let us see a example.

#include<iostream>
using namespace std;

int x = 5;

int main()
{
  const int &a = x;
  const int *b = &x;

  const_cast<int&> (a) = 6;
  cout << x << endl;

  *const_cast<int*> (b) = 7;
  cout << x << endl;

  return 0;
}

In the above example we are casting away the constness of pointer b and reference a. The pointer b and reference a are pointing to a non const variable x.

  • It would be undefined behavior if we cast away the constness of pointer or  reference those are pointing to a const variables. Let us see below program.
#include<iostream>
using namespace std;

const int x = 5;

int main()
{
  const int &a = x;
  const int *b = &x;

  const_cast<int&> (a) = 6;
  cout << x << endl;

  *const_cast<int*> (b) = 7;
  cout << x << endl;

  return 0;
}

Try to compile above program. it will compile without any error. try to run , it will error out core dumped at runtime. So if a variable in c++ is const , it should never be changed.

A simple use case of const_cast in c++

Let see below sample program.

#include<iostream>
using namespace std;

void foo(int* p) {
  cout << *p << endl;
}

int main()
{
  const int x = 5;

  foo(&x);
  return 0;
}
  • Let us compile the above program and see what errors come
bosch@bosch-Inspiron-N5050:~$ g++ test.cpp -otestexe
test.cpp: In function ‘int main()’:
test.cpp:12:9: error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive]
   foo(&x);
         ^
test.cpp:4:6: error:   initializing argument 1 of ‘void foo(int*)’ [-fpermissive]
 void foo(int* p) {
      ^

The error says that the program tries a conversion from const int* to int * because our foo function expects a non const pointer. so what should be options …

We have to take a const pointer from variable x, remove the constness of pointer and pass to the function foo().

#include<iostream>
using namespace std;

void foo(int* p) {
  cout << *p << endl;
}

int main()
{
  const int x = 5;

  const int *y = &x;
  int *z = const_cast<int*> (y);

  foo(z);

  return 0;
}
  • It is safe to use const_cast in place of simple cast.
#include<iostream>
using namespace std;

int main()
{
  int x = 5;

  const int &y = x;
  char &z = const_cast<char&> (y);

  return 0;
}
bosch@bosch-Inspiron-N5050:~$ g++ test.cpp -otestexe
test.cpp: In function ‘int main()’:
test.cpp:9:33: error: invalid const_cast from type ‘const int*’ to type ‘char*’
   char &z = const_cast<char&> (y);
                                 ^

The cast would give compile time error if the type of cast is not same as original variable.

  • const_cast example to cast away volatile-ness of a variable.
#include<iostream>
using namespace std;

void foo(int &ref)
{
  cout << ref << endl;
}
int main()
{
  int x = 5;

  volatile int &y = x;
  int &z = const_cast<int&> (y);
  foo(z);
  return 0;
}

 




Related Contents to follow