Search⌘ K

The volatile Keyword

Explore the volatile keyword in C++20 to understand when to use it, which features are deprecated, and the reasons behind those changes. Learn its role in preventing aggressive optimizations and discover why it does not support multithreading semantics, based on updated standards and practical examples.

We'll cover the following...

Proposal

The abstract in the proposal P1152R0 gives a short description of the changes that volatile undergoes:

“The proposed deprecation preserves the useful parts of volatile and removes the dubious / already broken ones. This paper aims at breaking at compile-time code which is today subtly broken at run time or through a compiler update.”

Before I dive into volatile, I want to answer the crucial question: When should you use volatile? A note from the C++ standard says that “volatile is a hint to the implementation to avoid aggressive optimization involving the object because the value of the object might be changed by ...