Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      cv(const andvolatile) type qualifiers

      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
      const/volatile
      decltype(C++11)
      auto(C++11)
      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
       
      Declarations
       

      Appear in any type specifier, includingdecl-specifier-seq ofdeclaration grammar, to specify constness or volatility of the object being declared or of the type being named.

      • const - defines that the type isconstant.
      • volatile - defines that the type isvolatile.

      Contents

      [edit]Explanation

      Any (possiblyincomplete) type other thanfunction type orreference type is a type in a group of the following four distinct but related types:

      • Acv-unqualified version.
      • Aconst-qualified version.
      • Avolatile-qualified version.
      • Aconst-volatile-qualified version.

      These four types in the same group have the samerepresentation andalignment requirements.

      Array types are considered to have the same cv-qualification as their element types.

      [edit]const and volatile objects

      When an object is first created, the cv-qualifiers used (which could be part ofdecl-specifier-seq or part of adeclarator in adeclaration, or part oftype-id in anew-expression) determine the constness or volatility of the object, as follows:

      • Aconst object is
      • an object whose type is const-qualified, or
      • a non-mutable subobject of a const object.
      Such object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.
      • Avolatile object is
      • an object whose type is volatile-qualified,
      • a subobject of a volatile object, or
      • amutable subobject of a const-volatile object.
      Every access (read or write operation, member function call, etc.) made through a glvalue expression of volatile-qualified type is treated as a visible side-effect for thepurposes of optimization (that is, within a single thread of execution, volatile accesses cannot be optimized out or reordered with another visible side effect that issequenced-before or sequenced-after the volatile access. This makes volatile objects suitable for communication with asignal handler, but not with another thread of execution, seestd::memory_order). Any attempt to access a volatile object through aglvalue of non-volatile type (e.g. through a reference or pointer to non-volatile type) results in undefined behavior.
      • Aconst volatile object is
      • an object whose type is const-volatile-qualified,
      • a non-mutable subobject of a const volatile object,
      • a const subobject of a volatile object, or
      • a non-mutable volatile subobject of a const object.
      Behaves as both a const object and as a volatile object.

      Each cv-qualifier (const andvolatile) can appear at most once in any cv-qualifier sequence. For example,constconst andvolatileconstvolatile are not valid cv-qualifier sequences.

      [edit]mutable specifier

      • mutable - permits modification of the class member declared mutable even if the containing object is declared const (i.e., the class member is mutable).

      May appear in the declaration of a non-staticclass members of non-reference non-const type:

      class X{    mutableconstint* p;// OK    mutableint*const q;// ill-formed    mutableint&       r;// ill-formed};

      mutable is used to specify that the member does not affect the externally visible state of the class (as often used for mutexes, memo caches, lazy evaluation, and access instrumentation).

      class ThreadsafeCounter{    mutablestd::mutex m;// The "M&M rule": mutable and mutex go togetherint data=0;public:int get()const{std::lock_guard<std::mutex> lk(m);return data;} void inc(){std::lock_guard<std::mutex> lk(m);++data;}};

      [edit]Conversions

      There is partial ordering of cv-qualifiers by the order of increasing restrictions. The type can be saidmore orless cv-qualified than:

      • unqualified <const
      • unqualified <volatile
      • unqualified <constvolatile
      • const <constvolatile
      • volatile <constvolatile

      References and pointers to cv-qualified types can be implicitly converted to references and pointers to more cv-qualified types, seequalification conversions for details.

      To convert a reference or a pointer to a cv-qualified type to a reference or pointer to a less cv-qualified type,const_cast must be used.

      [edit]Notes

      Theconst qualifier used on a declaration of a non-local non-volatilenon-template(since C++14)non-inline(since C++17) variable that is not declaredextern gives itinternal linkage. This is different from C where const file scope variables have external linkage.

      The C++ language grammar treatsmutable as astorage-class-specifier, rather than a type qualifier, but it does not affect storage class or linkage.

      Some uses of volatile are deprecated:

      (since C++20)

      [edit]Keywords

      const,volatile,mutable

      [edit]Example

      Run this code
      #include <cstdlib> int main(){int n1=0;// non-const objectconstint n2=0;// const objectintconst n3=0;// const object (same as n2)volatileint n4=0;// volatile object conststruct{int n1;        mutableint n2;} x={0,0};// const object with mutable member     n1=1;// OK: modifiable object//  n2 = 2;   // error: non-modifiable object    n4=3;// OK: treated as a side-effect//  x.n1 = 4; // error: member of a const object is const    x.n2=4;// OK: mutable member of a const object isn't const constint& r1= n1;// reference to const bound to non-const object//  r1 = 2; // error: attempt to modify through reference to constconst_cast<int&>(r1)=2;// OK: modifies non-const object n1 constint& r2= n2;// reference to const bound to const object//  r2 = 2; // error: attempt to modify through reference to const//  const_cast<int&>(r2) = 2; // undefined behavior: attempt to modify const object n2 [](...){}(n3, n4, x, r2);// see also: [[maybe_unused]] std::system("g++ -O3 -Wa,-adhln ./main.cpp");// may issue asm on POSIX systems}

      Possible output:

      # typical machine code produced on an x86_64 platform# (only the code that contributes to observable side-effects is emitted)main:    movl    $0, -4(%rsp) # volatile int n4 = 0;    movl    $3, -4(%rsp) # n4 = 3;    xorl    %eax, %eax   # return 0 (implicit)    ret

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1428C++98the definition of 'const object' was based on declarationbased on object type
      CWG 1528C++98there was no requirement on the number of occurrences
      of each cv-qualifier in the same cv-qualifier sequence
      at most once for
      each cv-qualifier
      CWG 1799C++98mutable could be applied to data members not declared
      const, but the members' types may still be const-qualified
      cannot applymutable to data
      members of const-qualified types

      [edit]See also

      C documentation forconst qualifier
      C documentation forvolatile qualifier
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/cv&oldid=174677"

      [8]ページ先頭

      ©2009-2025 Movatter.jp