The const_cast operator (C++ only)

A const_cast operator adds or removes a const or volatile modifier to or from a type.

Read syntax diagramSkip visual syntax diagram
const_cast operator syntax

>>-const_cast--<--Type-->--(--expression--)--------------------><

C++11With the right angle bracket feature, you may specify a template_id as Type in the const_cast operator with the >> token in place of two consecutive > tokens. For details, see Class templates (C++ only).C++11

The result of const_cast<Type>(expression) belongs to one of the following value categories:
  • If Type is an lvalue reference to an object type, const_cast<Type>(expression) is an lvalue.
  • C++11If Type is an rvalue reference to an object type, const_cast<Type>(expression) is an xvalue.C++11
  • In all other cases, const_cast<Type>(expression) is a C++11(prvalue)C++11 rvalue.

Type and the type of expression may only differ with respect to their const and volatile qualifiers. Their cast is resolved at compile time. A single const_cast expression may add or remove any number of const or volatile modifiers.

If a pointer to T1 can be converted to a pointer to T2 using const_cast<T2>, where T1 and T2 are object types, you can also make the following types of conversions:
  • An lvalue of type T1 to an lvalue of type T2 using const_cast<T2&>
  • C++11An lvalue or xvalue of type T1 to an xvalue of type T2 using const_cast<T2&&>C++11
  • C++11A prvalue of class type T1 to an xvalue of type T2 using const_cast<T2&&>C++11
If a conversion from a C++11(prvalue)C++11 rvalue of type pointer to T1 to type pointer to T2 casts away constness, the following types of conversions also cast away constness:
  • An lvalue of type T1 to an lvalue of type T2
  • C++11An expression of type T1 to an xvalue of type T2C++11
  • A C++11(prvalue)C++11rvalue of type pointer to data member of X of type T1 to type pointer to data member of Y of type T2

Types cannot be defined within const_cast.

The following demonstrates the use of the const_cast operator:

#include <iostream>
using namespace std;

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

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

  // Function f() expects int*, not const int*
  //   f(b);
  int* c = const_cast<int*>(b);
  f(c);

  // Lvalue is const
  //  *b = 20;

  // Undefined behavior
  //  *c = 30;

  int a1 = 40;
  const int* b1 = &a1;
  int* c1 = const_cast<int*>(b1);

  // Integer a1, the object referred to by c1, has
  // not been declared const
  *c1 = 50;

  return 0;
}
The compiler does not allow the function call f(b). Function f() expects a pointer to an int, not a const int. The statement int* c = const_cast<int>(b) returns a pointer c that refers to a without the const qualification of a. This process of using const_cast to remove the const qualification of an object is called casting away constness. Consequently the compiler does allow the function call f(c).

The compiler would not allow the assignment *b = 20 because b points to an object of type const int. The compiler does allow the *c = 30, but the behavior of this statement is undefined. If you cast away the constness of an object that has been explicitly declared as const, and attempt to modify it, the results are undefined.

However, if you cast away the constness of an object that has not been explicitly declared as const, you can modify it safely. In the above example, the object referred to by b1 has not been declared const, but you cannot modify this object through b1. You may cast away the constness of b1 and modify the value to which it refers.