易失性指针的迭代器特征

发布于 2025-01-20 11:13:23 字数 706 浏览 0 评论 0原文

该代码

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩世 2025-01-27 11:13:23

将@康桓玮的链接评论扩展为答案:

这是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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文