The best defense I can come up with is that its non-obvious to someone who has had to deal with its context-dependency in the compiler. In C++ it isn't necessarily even a read. int& a = *b; is more like a cast than a read or a write.
But as a user, this is just one of many context-dependent expressions we deal with as a matter of habit in C++. The expression *vp;, or even better (void)*vp; is obviously a read to me.
Sure, but I don't see this confusion to being limited to volatile. Are we suggesting that every time we want to do a copy we was to write read(ptr) instead of simply *ptr?
Dereferencing pointers is c(++) 101, imo. To me, this is in the same vain as observer_ptr
Dereferencing a pointer has never guaranteed that any physical read takes place because of the as-if rule. It is very easy to convince yourself that this is also the case in practice. Fire up godbolt and write some code that does that and you will detect that no compiler with optimisations turned on will do anything.
Yes I see that and no I did not check on godbolt before answering. This was overconfidence on my side and bad style. I do still not see, however, where in the standard it is stated that *vp should require a read. In my understanding *vp is a reference to int, not an int, and a compiler should not be required to read anything. Do you have a reference from the standard that indicates that I am wrong? auto dummy = *vp is another matter of course. I would prefer having a small function that makes it clear that I read from a specific variable such as inline void read_from(int const volatile& i) { auto [[maybe_unused]]dummy = i; }
7
u/gruehunter Oct 19 '19
The best defense I can come up with is that its non-obvious to someone who has had to deal with its context-dependency in the compiler. In C++ it isn't necessarily even a read.
int& a = *b;is more like a cast than a read or a write.But as a user, this is just one of many context-dependent expressions we deal with as a matter of habit in C++. The expression
*vp;, or even better(void)*vp;is obviously a read to me.