【问题标题】:Immediate function as default function argument initializer in Clang++立即函数作为 Clang++ 中的默认函数参数初始化器
【发布时间】:2021-10-17 17:55:26
【问题描述】:

如果使用立即函数(用consteval 声明)来默认初始化全局函数参数,就像这里

consteval int foo() { return 0; }
int bar(int a = foo()) { return a; }
int main() { return bar(); }

然后 Clang 编译器发出错误:

error: cannot take address of consteval function 'foo' outside of an immediate invocation
int bar(int a = foo()) { return a; }
                ^

当其他编译器接受代码时,演示:https://gcc.godbolt.org/z/z8h7MPaGx

假设代码格式正确并且只是一个 Clang 错误是否正确?是否有任何已知的解决方法?

【问题讨论】:

标签: c++ c++20 clang++ consteval


【解决方案1】:

是的,这是一个 Clang 错误;考虑到 std::source_location::current 是 consteval 并且正是为了这种用法。

【讨论】:

  • 谢谢。据我所知,std::source_location 还没有在 Clang 中实现。你的意思是这个错误是造成这种情况的原因吗?
  • @Fedor:大概是一个这样的原因——不过,这个功能在更多方面非常神奇。
猜你喜欢
  • 2017-11-19
  • 1970-01-01
  • 2011-05-13
  • 2021-11-27
  • 2014-10-05
  • 2019-09-11
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
相关资源
最近更新 更多