cpp/language/cv

Appear in any type specifier, including of , to specify constness or volatility of the object being declared or of the type being named.


 * - defines that the type is constant.
 * - defines that the type is volatile.

Explanation
For any type (including incomplete types), other than  or, there are three more distinct types in the C++ type system:  ,  , and.


 * Note: are considered to have the same cv-qualification as their element types.

When an object is first created, the cv-qualifiers used (which could be part of or part of a  in a, or part of  in a ) determine the constness or volatility of the object, as follows:


 * const object - an object whose type is, 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.
 * volatile object - an object whose type is, or a subobject of a volatile object, or a mutable 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 the (that is, within a single thread of execution, volatile accesses cannot be optimized out or reordered with another visible side effect that is  or sequenced-after the volatile access. This makes volatile objects suitable for communication with a signal handler, but not with another thread of execution, see std). Any attempt to access a volatile object through a  of non-volatile type (e.g. through a reference or pointer to non-volatile type) results in undefined behavior.
 * const volatile object - an object whose type is, 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 ( and ) can appear at most once in any cv-qualifier sequence. For example, and  are not valid cv-qualifier sequences.

mutable specifier

 * - permits modification of the class member declared mutable even if the containing object is declared const.

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

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).

Conversions
There is partial ordering of cv-qualifiers by the order of increasing restrictions. The type can be said more or less cv-qualified than:


 * unqualified <
 * unqualified <
 * unqualified <

References and pointers to cv-qualified types may be to references and pointers to more cv-qualified types. In particular, the following conversions are allowed:


 * reference/pointer to unqualified type can be converted to reference/pointer to
 * reference/pointer to unqualified type can be converted to reference/pointer to
 * reference/pointer to unqualified type can be converted to reference/pointer to
 * reference/pointer to type can be converted to reference/pointer to
 * reference/pointer to type can be converted to reference/pointer to


 * Note: are imposed on multi-level pointers.

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

Keywords
, ,