4

If one uses an immediate function (declared with consteval) for default initialization of a global function argument like here

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

then Clang compiler issues the error:

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

while other compilers accept the code, demo: https://gcc.godbolt.org/z/z8h7MPaGx

Is it right to assume that the code is well formed and it is simply a Clang bug? Is there any known workaround for it?

Fedor
  • 17,146
  • 13
  • 40
  • 131

1 Answers1

4

Yes, this is a Clang bug; consider that std::source_location::current is consteval and is meant for exactly this usage.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • Thanks. As far as I know, `std::source_location` is not implemented in Clang yet. Do you mean that this bug is the reason for that? – Fedor Aug 15 '21 at 11:22
  • 1
    @Fedor: It’s presumably *one* such reason—that function is very magical in more respects than that, though. – Davis Herring Aug 15 '21 at 16:40