skip to content

Which volatile operations did C++20 deprecate and C++23 restore, and is reg |= TX_ENABLE on a volatile register still deprecated?

level: seniorimportance: should knowfreq 35%

answer

  1. operations that look atomic
  2. increment and decrement stay deprecated
  3. compound assignment restored as a defect report
  4. using an assignment's value
  5. volatile parameters and returns

basics

~20 s

No. C++20 deprecated ++, --, compound assignment, using an assignment's result, and volatile parameters, returns and structured bindings. Defect reports for C++23 restored compound assignment, so reg |= TX_ENABLE as a statement is fine; ++reg stays deprecated.

solid answer

~50 s

C++20 (paper P1152) deprecated the `volatile` uses that suggest more than they deliver: prefix and postfix `++` and `--` on volatile arithmetic and pointer types, compound assignment such as `reg |= MASK`, using the result of an assignment to a volatile object (`x = (reg = 5)`), function parameters and return types of volatile-qualified type, and volatile structured bindings. Embedded code relies on `reg |= MASK`, so WG21 restored it: P2327R1 un-deprecated the bitwise compound assignments and then CWG2654 un-deprecated all compound assignments, both as defect reports that compilers also apply in C++20 mode. In the C++23 draft only an assignment whose value is used is deprecated, so `reg |= TX_ENABLE;` and `reg += 2;` as statements are fine, while `++reg` still warns. The C++26 draft keeps the same list. Clang 21 agrees: no warning for compound assignments as statements, and `-Wdeprecated-volatile` warnings for `++reg` and a used assignment.

code

cpp · 9 lines
cpp
#include <cstdint>
volatile std::uint32_t reg = 0;
int main() {
  reg |= 0x1u;         // bitwise compound assignment
  reg += 2u;           // arithmetic compound assignment
  ++reg;               // increment
  std::uint32_t x = (reg = 5u);  // assignment used as a value
  return static_cast<int>(x);
}

go deeper

for a junior

Know that ++ on a volatile variable warns from C++20 on, and that reg |= MASK is fine again.

for a middle

List what C++20 deprecated and which part the defect reports restored, with the reason each looked misleading.

for a senior

Clean up a -Werror build after a standard upgrade, choosing between atomics and explicit two-access rewrites.

for a principal

Plan the language-standard upgrade for shared C and C++ register headers, and decide whether deprecation warnings become errors.

## Why C++20 deprecated parts of volatile `volatile` has one job: make each access to an object happen, for memory-mapped hardware and similar uses. Several operators on volatile objects look like a single hardware operation but are really several accesses, and some declarations using `volatile` mean nothing useful. Paper **P1152R4**, adopted for C++20, deprecated them so that compilers would warn: | Construct | Example | Why it misleads | |---|---|---| | Prefix or postfix `++` / `--` | `++rx_count;` | reads like one indivisible step, is a load and a store | | Compound assignment | `reg \|= TX_ENABLE;` | a load, an operation and a store | | Using an assignment's result | `x = (reg = 5);` | does `x` get 5, or a re-read of the register? | | Volatile parameter or return type | `void f(volatile int v);` | a by-value copy is not hardware | | Volatile structured binding | `volatile auto [a] = s;` | unclear which object is volatile | Volatile pointer parameters such as `void f(volatile Regs* r)` were never affected; the pointee being volatile is the normal, useful case. ## Why part of it came back Firmware and vendor headers are full of `reg |= MASK;` and `reg &= ~MASK;`. Rewriting them as `reg = reg | MASK;` changes nothing in the generated code, and much of that code is shared with C, where the operators are not deprecated. Two defect reports reversed the compound-assignment part: 1. **P2327R1**, "De-deprecating volatile compound operations", accepted as a defect report in July 2022, restored the bitwise compound assignments `|=`, `&=` and `^=`. 2. **CWG2654**, "Un-deprecation of compound volatile assignments", accepted as a defect report in November 2022 in response to a national-body comment, restored the rest, including `+=`. Because both are **defect reports**, they apply retroactively, and compilers implement them in C++20 mode as well. ## What the C++23 and C++26 drafts say The C++23 draft's assignment wording is a single sentence: an assignment whose left operand is of volatile-qualified type is deprecated unless it is a discarded-value expression or an unevaluated operand. Simple and compound assignment are both covered, so in practice: - `reg |= TX_ENABLE;` as a statement: **not deprecated**. - `reg += 2;` as a statement: **not deprecated**. - `x = (reg = 5);` or `if ((reg |= MASK) & READY)`: **deprecated**, because the assignment's value is used. - `++reg;`, `reg--;`: **still deprecated** for volatile arithmetic and pointer types. - Volatile parameters, return types and structured bindings: **still deprecated**. The deprecation annex of the C++23 draft illustrates it with `brachiosaur += neck; // OK` and `tail = brachiosaur = neck; // deprecated`. The C++26 final draft keeps the same text, so nothing on this list was removed outright. ## What the compiler does Compiling one file with Apple clang 21 in each mode (code example below) gives no warnings for C++17, and from C++20 on exactly two `-Wdeprecated-volatile` warnings: the increment and the used assignment. The compound assignments never warn, which matches the defect reports. Deprecated is not removed: the code still compiles, but a project that builds with `-Werror` will fail. ## Finding the deprecated uses in a large codebase A standard upgrade usually surfaces these all at once: 1. Build once with the new `-std` and collect every `-Wdeprecated-volatile` warning (Clang's name for the group); fix them before turning warnings into errors. 2. Expect most hits in shared register headers and in interrupt counters, not in application code. 3. Check C headers included from C++: the same line can be fine in C and deprecated in C++, so a macro such as `#define SET_BIT(r, m) ((r) |= (m))` used as a value still warns. ## Rewriting deprecated register code - `++counter;` on a volatile counter: if it is shared with an interrupt, replace it with a lock-free `std::atomic` and `fetch_add`; if it is a hardware register, write `reg = reg + 1;` so both accesses are visible. - `if ((reg = v) == 0)`: split it into a store and a separate volatile read, and decide which one you meant. - `void set(volatile int v)`: drop the qualifier from the parameter; it never affected the caller.

  • Why did C++20 deprecate volatile function parameters but not pointers to volatile?
    A by-value `volatile int` parameter is a private copy the caller never sees, so `volatile` on it has no hardware meaning and only confuses readers. A `volatile Regs*` parameter points at the real hardware, where every access must happen, which is the useful case, so it was left alone.
  • How does a defect report differ from an ordinary change in a later standard?
    A defect report fixes the earlier standard retroactively: WG21 treats the corrected rule as what the older standard should have said, so compilers apply it in the older modes too. That is why Clang does not warn about `reg |= MASK` even with `-std=c++20`.

  • reg |= MASK on a volatile register is deprecated in C++23.
  • C++20 removed volatile from the language.
  • C++23 un-deprecated ++ and -- on volatile objects.
  • Pointers to volatile were deprecated as function parameters.
  • Deprecated volatile operations no longer compile in C++20.

Assumes C++20 deprecations (P1152R4) and the C++23 defect reports P2327R1 and CWG2654; checked against the C++23 (N4950) and C++26 (N5050) drafts.