易失性指针的迭代器特征
该代码
#include <iterator>
#include <type_traits>
static_assert(std::is_same_v<typename std::iterator_traits<volatile int *>::value_type, volatile int>);
在最新的GCC和Clang上编译,但在MSVC 2019上失败了,这似乎删除了volatile
限定符。参见在Godbolt上。
由于“ nofollow noreferrer”> std: :iterator_traits 对于const t*
和t*
,但我认为volatile
应该保留。
谁是对的?
编辑:我正在使用C ++ 17编译。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将@康桓玮的链接评论扩展为答案:
这是LWG 问题 2952。在其解析之前,
value_type
将是易失性
限定的,但其解析会更改它以删除易失性
限定。该决议已合并到 C++20 中,MSVC、GCC 和 Clang 似乎都这样实现了它。 (这意味着当编译器设置为 C++20 模式时,问题中的
static_assert
会失败。)关于该解决方案是否应作为缺陷报告应用于标准的先前修订版,您可以可以在这里阅读一些讨论: https://github.com/microsoft/STL/issues/2612。
看来 Microsoft 的标准库实现以及 LLVM 的 libc++ 也将问题解决方案应用于以前的标准修订模式,而 GCC 的 libstdc++ 则不然。我找不到任何错误报告或类似的讨论后者的选择。
Extending the link comment by @康桓瑋 to an answer:
This is LWG issue 2952. Before its resolution
value_type
would bevolatile
-qualified, but its resolution changes it to remove thevolatile
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.