I do have a program which makes use of volatile-qualified std::chrono::duration
objects. Apparently, such objects become very hard to use. For example, following simple program produces compilation error (on gcc and clang):
#include <chrono>
volatile std::chrono::nanoseconds s;
void foo(std::chrono::nanoseconds k) {
s = k;
}
error: passing 'volatile nanoseconds' {aka 'volatile std::chrono::duration >'} as 'this' argument discards qualifiers [-fpermissive]
It is obvious why I have this error given the interface of the class, and I also know how to hakishly make it "work" (undefined-behavior wise) with const_cast
.
Having said that, can absence of volatile-qualified members of std::chrono::duration
considered to be a defect? After all, volatile
is a perfectly legal C++ construct, and although rare in use, has it's applications.
P.S. I would like to leave the wisdom of volatile-qualification outside of this question, but my crystal ball tells me it is not to be, so to preempt all 'you do not need volatile because it is not thread-safe' mantra, let's consider the case of a signal handler.
volatile
is not required to work properly with a signal handler. Prior to 2011 the only acceptable type wassig_atomic_t
. In C++ 11 you can also use various atomic types. – Pete Becker