Search code examples
c++c++20clang++consteval

Immediate function as default function argument initializer in Clang++


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?


Solution

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