Why does a const reference to volatile int need a static_cast?

c++, volatile

Solution

"Volatility" of types in C++ works in pretty much exactly the same way as "const-ness" -- that is, an object that is `volatile` cannot be assigned to a non-`volatile` reference, in just the same way that

const int i = 3;
int& j = i; // won't work

Similarly, only methods marked `volatile` can be called on a volatile object:

struct S
{
    void do_something() volatile {}
    void do_something_else() {}
};

volatile S s;
s.do_something(); // fine
s.do_something_else(); // won't work

Methods can be overloaded by "volatility", in the same way they can be overloaded by const-ness.

In C++ standardese, these things are known as cv-qualifiers, to emphasise that they work in exactly the same way. (C99 adds a third that works in the same way, `restrict`, available as an extension in some C++ compilers). You can change cv qualifiers using `const_cast<>` -- the more powerful `static_cast<>` isn't required.

EDIT:

Just to make clear, a type can have both `const` and `volatile` modifiers at the same time, giving a total of four possibilities:

int i;
const int j;
volatile int k;
const volatile int l;

Problem

Given the following code: ``` struct Foo { volatile int i; }; const int& bar = foo.i; ``` I get: ``` error: invalid initialization of reference of type 'const int&' from expression of type 'volatile int' ``` Unless I provide a `static_cast<volatile int>`.

Original source

Related problems