c++ consteval打破了?在苹果叮当声++

8hhllhi2  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(94)

What doe the diagnostic below mean? I do not understand how a call to a correct consteval function with no arguments can possibly be anything other than a constant expression.

#include <iostream>
template<int x>
struct X {
consteval static int get() { return x; }
int f() const { return get(); }
};

int main() {
  ::std::cout << X<4>().get() << ::std::endl;
}
~/felix>clang++ -std=c++20 -ferror-limit=1 xx.cxx
xx.cxx:4:17: error: call to consteval function 'X::get' is not a constant expression
int f() const { return get(); }
                        ^

clang is confused. Here's the proof: this works:

int f() const { int a[get()]; return sizeof(a); }

clang thinks a consteval function can ONLY be used in a context where a constant expression is required: whether or not that is the rule in the Standard it's nonsense. In my actual code, the function is successfully called without a diagnostic if qualified by a template typename parameter, and is used in a context which does not require a constant.

ycl3bljg

ycl3bljg1#

程序格式良好,这是一个clang错误,在clang 15.0中为fixed
Demo

相关问题