CKTIS / C++
volatile — type qualifier
`volatile` qualifies a type and gives accesses through such expressions observable significance. It can support hardware access under implementation-specific rules, but it provides neither atomicity nor inter-thread synchronization.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
volatile T object;
T volatile object;
volatile T* pointer_to_volatile;
T* volatile volatile_pointer = nullptr;
const volatile T* read_only_volatile_pointer;volatile is a type qualifier, like const. The spellings volatile T and T volatile are equivalent. Accesses through volatile-qualified glvalue expressions are part of the program's observable behavior and must follow the abstract machine's rules. This is not a general switch that disables optimization.
The detailed semantics of such access are implementation-defined. Memory-mapped hardware registers are a typical use, but valid addresses, access widths, and required barriers depend on the platform. volatile alone guarantees neither a particular processor instruction nor cache bypass. Accessing an object defined as volatile through a non-volatile glvalue causes undefined behavior.
In volatile int* p, the qualifier applies to the pointed-to int; in int* volatile p, it applies to the pointer itself, not the pointed-to object. const volatile int* p prevents modification through *p while retaining volatile access semantics.
volatile provides neither atomicity nor inter-thread synchronization. It does not make a shared flag safe: a data race still means undefined behavior. Use appropriate atomic operations or mutexes for communication between threads.
In C++23, volatile itself is not deprecated, but deprecated uses include ++ and -- on volatile-qualified arithmetic and pointer types, by-value parameters and function return types with top-level volatile, and structured binding declarations with volatile. The example uses ordinary assignment and reading. The supplied sources are an evolving standard draft, not a frozen C++23 publication.
Examples
#include <iostream>
int main() {
volatile int value = 7;
volatile int* p = &value;
*p = 9;
const int snapshot = *p;
std::cout << snapshot << '\n';
}
Result
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.