Reputation: 124
What is the reason that the compiler doesn't autogenerate the volatile assignment operator, thus preventing the assignment?
A minimal example:
struct A{};
A a;
volatile A b;
b = a;
Upvotes: 0
Views: 234
Reputation: 170203
I assume you mean why when you create a class
class C {
// assignment operator auto generated
};
The assignment operator you get is
C& operator=(C const&) = default;
and not
C volatile& operator=(C volatile const&) volatile = default;
So a volatile glvalue cannot be on either side.
The reason is likely cost related. Volatile objects inhibit certain optimizations, and the qualifier sticks to sub-objects (both under pain of const_cast
). They are also very rare in the wild, like really, practically no one ever declares volatile class objects. If C++ generated a volatile assignment operator by default, all code will need to pay a cost by default, even though they don't use volatile objects. But C++ is designed with the opposing philosophy, we don't pay for what we don't use.
If you are dealing with a library that necessitates the creation of volatile objects, and it breaks on this, then it is poorly designed. But honestly, it's far more likely the library never banked on having its objects declared volatile, and so perhaps it is you who should reconsider doing so.
Upvotes: 1