What's a C++03 compliant way of fetching a value from a volatile variable?
c++, language-lawyer
Solution
`int i = x;` should work. This code absolutely requires reading the volatile variable and the optimizer is not allowed to optimize the read away. But since the variable `i` is unused the optimizer can avoid any extra work involved in storing the read value.
You might also need something like this to avoid compiler warnings: `(void)i;`
Problem
According to this defect report C++03 Standard does not guarantee that in the following code: ``` volatile int x; void f() { x; } ``` the variable is read from. Then how do I write code that just read the volatile variable value and discards the result (read for the sake of read)?