Search code examples
c++c++11templatesstatic-assert

How to use std::is_volatile?


I am trying to disallow a specific operation on volatile types. To accomplish this I am trying to use std::is_volatile, but the code below is compiling without errors, which is not what I want.

Why is is_volatile::value false in the case below?

#include <type_traits>

template<typename T>
inline void DoStuff(T val) {
    static_assert(!std::is_volatile<T>::value, "No volatile types plz");
    //...
}

int main() {
    volatile char sometext[261];
    DoStuff(sometext);
}

Solution

  • The problem is that T isn't a volatile type at all. It's volatile char*. Wait a minute, you say, I see volatile right there. True, but consider this: char* volatile is a volatile type. volatile char* isn't. It's a non-volatile pointer to a volatile char array.

    Solution: std::is_volatile<typename std::remove_pointer<T>::type>