Search code examples
c++iteratorlanguage-lawyervolatile

Iterator traits on pointer to volatile


This code

#include <iterator>
#include <type_traits>

static_assert(std::is_same_v<typename std::iterator_traits<volatile int *>::value_type, volatile int>);

compiles on latest GCC and clang, but fails on MSVC 2019, that seems to remove volatile qualifier. See here on godbolt.

The const is removed, due to the standard specialization of std::iterator_traits for const T* and T*, but I think volatile should be kept.

Who is right?

Edit: I'm compiling with C++17.


Solution

  • Extending the link comment by @康桓瑋 to an answer:

    This is LWG issue 2952. Before its resolution value_type would be volatile-qualified, but its resolution changes it to remove the volatile qualification.

    The resolution is incorporated into C++20 and MSVC, GCC and Clang all seem to implement it as such. (Meaning that the static_assert in the question fails when the compiler is set to C++20 mode.)

    With regards to whether the resolution should be applied as a defect report to previous revisions of the standard, you can read some discussion here: https://github.com/microsoft/STL/issues/2612.

    It seems that Microsoft's standard library implementation as well as LLVM's libc++ apply the issue resolution also to previous standard revision modes, while GCC's libstdc++ doesn't. I could not find any bug report or similar discussing the latter's choice.