dod*_*ndi 6 c++ static-assert constexpr c++17
请使用以下代码:
#include <array>
constexpr std::array<int, 10> a{};
static_assert(std::next(std::begin(a)) == std::begin(a) + 1);
Run Code Online (Sandbox Code Playgroud)
随着-std=c++17GCC的完美编译,但Clang抱怨表达式不是一个完整的常量表达式.看起来问题是关于std::next哪个应该是constexpr在C++ 17中.
然而,std::next在std库中,而不是在编译器本身,因此有一些奇怪的事情发生.只是为了让事情变得更好,如果你传给-stdlib=libc++Clang ,那么这个例子就完美地编译了.
到底是怎么回事?谁错了,谁是对的?
编辑
这个问题似乎与使用Godbolt内部的GCC 7.2 工具链相关.如果将--gcc-toolchain=/opt/compiler-explorer/gcc-snapshot参数添加到命令行,一切都可以正常运行.(感谢@einpoklum向godbolt报告了这个问题 - 我速度慢了;))
编辑
对于那些认为旧编译器应该适用于实际标准的人来说,遗憾的是,这种考虑是没有意义的.我在谈论GCC和Clang的最新版本.并且这个问题可以通过两者的主干版本重现.较旧的编译器与此问题无关(相反,MSVC行为会很有趣).
在 Wandbox 中编译
#include <array>
int main()
{
constexpr std::array<int, 10> a{};
static_assert(std::next(std::begin(a)) == std::begin(a) + 1);
}
Run Code Online (Sandbox Code Playgroud)
使用 clang++ 4.0.1 和命令行
clang++ prog.cc -Wall -Wextra -std=c++1z
Run Code Online (Sandbox Code Playgroud)
我收到错误
prog.cc:6:18: error: static_assert expression is not an integral constant expression
static_assert(std::next(std::begin(a)) == std::begin(a) + 1);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:6:18: note: non-constexpr function 'next<const int *>' cannot be used in a constant expression
/opt/wandbox/clang-4.0.1/include/c++/v1/iterator:599:1: note: declared here
next(_InputIter __x,
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
但是使用 clang++ 5.0.0 (或 6.0.0 或 7.0.0 HEAD)进行编译
clang++ prog.cc -Wall -Wextra -std=c++17
Run Code Online (Sandbox Code Playgroud)
因此,clang++ 4.0.1(和/或 clang++ 使用的库)中对 C++17 的支持似乎较低,在以下版本中进行了更正。
- 编辑 -
gobold中的 clang++ 5.0.0 和 clang++ 6.0.0 已确认问题(请参阅 einpoklum 的答案)。
所以我认为问题是 libstdc++ 的版本:似乎gobold正在使用一个版本(我想是较旧的版本) where std::next()isn't 定义为constexprwandbox正在使用一个版本 where std::next()is constexpr。
| 归档时间: |
|
| 查看次数: |
358 次 |
| 最近记录: |