CKTIS / C++
const — read-only access qualifier
const restricts modification through a particular name, reference, or pointer. Learn the difference between a pointer to const and a const pointer, and what const after a member function's parameter list means.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
const T name = value;
T const name = value;
const T* pointer;
T* const pointer = address;
const T* const pointer = address;
const T& reference = object;
ReturnType method() const;const is a type qualifier. const int x = 3; and int const x = 3; mean the same thing: you cannot assign a new value to x. Such a scalar object requires initialization. const alone does not require compile-time evaluation; it is not a substitute for constexpr.
Qualifier placement matters for pointers. const int* p allows changing p but forbids writing through *p. For the following independent declarations, use a separate non-const object: int y = 3;. int* const p = &y; prevents changing the pointer itself but lets you modify y through *p. const int* const p = &y; combines both restrictions. const int& r = y; prevents writing through r without making y const: changes made through another access path remain visible through r.
In an implicit-object member function such as int get() const, the trailing const makes this point to a const-qualified object. The function can be called on a const object; it cannot directly modify that object's non-static data members other than mutable members. This is not deep immutability: an object pointed to by a pointer member does not automatically become const.
An ordinary attempt to write through const-qualified access is a compile-time error. Removing the qualifier with const_cast does not make modifying an actually const object legal: that modification causes undefined behavior. In the example, value remains non-const, so writing through fixed is allowed, and view and pointer read the updated value.
The example targets compilation with -std=c++23. The cited sources are an evolving working draft, not a frozen C++23 publication.
Examples
#include <iostream>
struct Reading {
int value;
int get() const { return value; }
};
int main() {
int value = 4;
const int& view = value;
const int* pointer = &value;
int* const fixed = &value;
*fixed = 7;
const Reading reading{9};
std::cout << view << ' ' << *pointer << ' ' << reading.get() << '\n';
}
Result
7 7 9
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.