Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      const_cast conversion

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
      Expressions
      General
      Literals
      Operators
      Conversions
       

      Converts between types with different cv-qualification.

      Contents

      [edit]Syntax

      const_cast<target-type>(expression)

      Returns a value of typetarget-type.

      [edit]Explanation

      Only the following conversions can be done withconst_cast:

      1) For twosimilar object pointer or pointer to data member typesT1 andT2, a prvalue of typeT1 can be converted toT2 ifT1 andT2 differ only in cv-qualification (formally, if, considering thequalification-decompositions of both types, eachP1_i is the same asP2_i for alli).
      • Ifexpression is a null pointer value, the result is also a null pointer value.
      • Ifexpression is a null member pointer value, the result is also a null member pointer value.
      • Ifexpression points to an object, the result points to the same object.
      • Ifexpression points past an object, the result points past the same object.
      • Ifexpression points to a data member, the result points to the same data member.

      Even ifexpression is a prvalue,temporary materialization is not performed.

      (since C++17)
      2) For twoobject typesT1 andT2, if a pointer toT1 can be explicitly converted to the type “pointer toT2” usingconst_cast<T2*>, then the following conversions can also be made:
      • An lvalue of typeT1 can be explicitly converted to an lvalue of typeT2 usingconst_cast<T2&>.
      • A glvalue of typeT1 can be explicitly converted to an xvalue of typeT2 usingconst_cast<T2&&>.
      • IfT1 is a class or array type, a prvalue of typeT1 can be explicitly converted to an xvalue of typeT2 usingconst_cast<T2&&>.
      (since C++11)

      The result reference refers to the original object.

      (until C++17)

      Ifexpression is a glvalue, the result reference refers to the original object. Otherwise, the result reference refers to thematerialized temporary.

      (since C++17)

      As with all cast expressions, the result is:

      • an lvalue iftarget-type is an lvalue reference type or an rvalue reference to function type(since C++11);
      • an xvalue iftarget-type is an rvalue reference to object type;
      (since C++11)
      • a prvalue otherwise.

      [edit]Casting away constness

      For two different typesT1 andT2, a conversion fromT1 toT2casts away constness if there exists aqualification-decomposition ofT2 of the form “cv2_0 P2_0 cv2_1 P2_1 ... cv2_n−1 P2_n−1 cv2_n U2”, and there is noqualification conversions that convertsT1 to “cv2_0 P1_0 cv2_1 P1_1 ... cv2_n−1 P1_n−1 cv2_n U1” (same cv-components, different P-components and U-components).

      If a cast from a prvalue of typeT1* to the typeT2* casts away constness, casting from an expression of typeT1 to a reference toT2 will also cast away constness.

      Onlyconst_cast may be used to cast away constness.

      “Casting away constness” implies “casting away volatility”, as qualification conversions cannot cast away volatility as well.

      [edit]Notes

      Pointers to functions and pointers to member functions are not subject toconst_cast.

      const_cast makes it possible to form a reference or pointer to non-const type that is actually referring to aconst object or a reference or pointer to non-volatile type that is actually referring to avolatile object. Modifying a const object through a non-const access path and referring to a volatile object through a non-volatileglvalue results in undefined behavior.

      [edit]Keywords

      const_cast

      [edit]Example

      Run this code
      #include <iostream> struct type{int i;     type(): i(3){} void f(int v)const{// this->i = v;                 // compile error: this is a pointer to constconst_cast<type*>(this)->i= v;// OK as long as the type object isn't const}}; int main(){int i=3;// i is not declared constconstint& rci= i;const_cast<int&>(rci)=4;// OK: modifies istd::cout<<"i = "<< i<<'\n';     type t;// if this was const type t, then t.f(4) would be undefined behavior    t.f(4);std::cout<<"type::i = "<< t.i<<'\n'; constint j=3;// j is declared const[[maybe_unused]]int* pj=const_cast<int*>(&j);// *pj = 4;      // undefined behavior [[maybe_unused]]void(type::* pmf)(int)const=&type::f;// pointer to member function// const_cast<void(type::*)(int)>(pmf);   // compile error: const_cast does// not work on function pointers}

      Output:

      i = 4type::i = 4

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1965C++11const_cast could not bind rvalue references to array prvaluesallowed to bind such references
      CWG 2879C++17pointer pvalue operands were materializedthey are not materialized

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 7.6.1.11 Const cast [expr.const.cast]
      • C++20 standard (ISO/IEC 14882:2020):
      • 7.6.1.10 Const cast [expr.const.cast]
      • C++17 standard (ISO/IEC 14882:2017):
      • 8.2.11 Const cast [expr.const.cast]
      • C++14 standard (ISO/IEC 14882:2014):
      • 5.2.11 Const cast [expr.const.cast]
      • C++11 standard (ISO/IEC 14882:2011):
      • 5.2.11 Const cast [expr.const.cast]
      • C++98 standard (ISO/IEC 14882:1998):
      • 5.2.11 Const cast [expr.const.cast]
      • C++03 standard (ISO/IEC 14882:2003):
      • 5.2.11 Const cast [expr.const.cast]

      [edit]See also

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/const_cast&oldid=179727"

      [8]ページ先頭

      ©2009-2025 Movatter.jp