Deprecating volatile - JF Bastien - CppCon 2019

CppCon · 30K views · 2019 · 1:00:54

Open on YouTube

C, C++, Rust, Go & systems languagesProgramming language designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when explaining why C++20 deprecated most uses of `volatile`, since it lays out the motivating ambiguous examples and the committee's reasoning.

Recommended by 3 people across 3 different months, 2019 to 2022.

What it is

A CppCon 2019 talk by JF Bastien, Apple compiler engineer and C++ language evolution chair, on why `volatile` is poorly specified and often misunderstood. He walks through the committee's plan to deprecate broken uses in C and C++ and to standardize safer replacements.

What people said

Recommends unDeadHerbs · 2019-12 · Stack Overflow · on “Why is most uses of volatile deprecated in C++20?”

There's a good talk by the C++ committee language evolution chair [this video] on why. Brief summary, the places that volatile is being removed from didn't have any well defined meaning in the standard and just caused confusion. Motivating (Ambiguous) Examples Volatile bit Fields should be specified by your hardware manual and/or compiler. Is += a single/atomic instruction? How about ++ ? How many reads/writes are needed for compare_exchange ? What if it fails? What does void foo(int volatile n) mean? or int volatile foo() ? Should *vp; do a load? (This has changed twice in the standard.) Threading Historically, people have used volatile to achieve thread safety in C and C++. In C++11, non-UB way

Recommends David Grayson · 2021-12 · Stack Overflow · on “WHY deprecate the keyword "volatile" in C++20, although it has important uses in embedded programming?”

@user17732522 The C++ language standard (in the "expr.ass" section) made pretty clear that those two lines were equivalent, and l think most programmers understood that. There is no objective reason why one of those lines needs to be deprecated and not the other, but I guess we just have to watch the one-hour talk by JF Bastien [this video] to see why he wanted to change the standard.

Recommends Maciej Polański · 2022-11 · Stack Overflow · on “Can one volatile constexpr variable initialize another one in C++?”

The defect report you linked shows it should not work, so Clang is correct. (...) “a non-volatile object defined with constexpr” (...) is permitted but that such a variable cannot appear in a constant expression . What is the intent? But more interesting is: why does Clang care while other compilers don't? In my opinion, this happened because of JF Bastien [LINK] , a very influential figure in the world of Clang / LLVM, that personally dislikes volatile :) He has been proposing to remove [LINK] it from the language for a long time. So if it was allowed to ban volatile somewhere, he probably spared no effort to make it so. If for no other reason than simply to prevent people from writing code

Cites unDeadHerbs · 2021-04 · Stack Overflow · on “Why is most uses of volatile deprecated in C++20?”

In his discussion at the end he mentions that they won't be removing volatile-qualified member functions yet because there are still uses for them. The goal is for there to be a better way to describe what "volatile correctness" is trying to do. [this video] Also, if there's a specific use of this needed for a hardware platform, I believe the compiler is allowed to implement the feature anyway as behavior that is undefined by the standard. The short of it though is that `volatile` isn't for threading.

Cites Albert -Al- Hollmann · 2021-12 · Stack Overflow · on “WHY deprecate the keyword "volatile" in C++20, although it has important uses in embedded programming?”

Update: First of all, please kindly note that this question does neither involve threading, nor is it about re-ordering or fence-related. Therefore, this does not answer my question (although it's a similar question with an accepted answer): Why is volatile deprecated in C++20? [LINK] I guess that there is something important worth clarifying: If you want your code thread-safe, then in many cases, it's not sufficient, to ONLY use volatile. Additional or alternative techniques are required (atomic, lock, etc.). Anyway this question is not about thread-safety. I recently started my first huge embedded C++ project (with some parts still in C), after many years of pure C with some (inline-)assem

Cites David Grayson · 2021-12 · Stack Overflow · on “WHY deprecate the keyword "volatile" in C++20, although it has important uses in embedded programming?”

I watched JF Bastien's talk [this video] and he didn't give any examples of people other than himself who were confused by what `|=` means.