在 CompilerExplorer 上使用这个小代码片段和 std=c++20 for x86-64 clang 11.0.0 编译并运行。
#include <algorithm>
#include <array>
consteval std::array<double,2> gof()
{
std::array<double,2> x{ 3., 2.};
std::reverse(begin(x),end(x));
return x;
}
int
main(int argc, char *argv[])
{
return gof().at(0);
}
但是在具有相同设置的 CompilerExplorer 上的 clang-tidy 会产生
clang-tidy (trunk) #1 with x86-64 clang 11.0.0
<source>:14:12: error: call to consteval function 'gof' is not a constant expression [clang-diagnostic-error]
return gof().at(0);
^
<source>:7:5: note: non-constexpr function 'reverse<double *>' cannot be used in a constant expression
std::reverse(begin(x),end(x));
^
<source>:14:12: note: in call to 'gof()'
return gof().at(0);
^
/opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/bits/stl_algo.h:1180:5: note: declared here
reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
^
整洁的产量是我所期望的。为什么clang处理它的方式不同?