c++gccc++20constevalstd-source-location

consteval wrapper vs. source_location


I tried the following first variant using GCC10 in C++20 mode:

consteval std::experimental::source_location there()
{
   return std::experimental::source_location::current(); // Line 3
}

void f(const std::experimental::source_location& a = there()) // Line 6
{
   std::cout << a.line() << std::endl;
}

int main(int pArgc, char* pArgv[])
{
   std::cout << there().line() << std::endl; // Line 13
   f(); // Line 14

   return 0;
}

I expected the following output:

13
14

but I got:

3
3

Then I tried the following second variant:

consteval std::experimental::source_location there(const std::experimental::source_location& a = std::experimental::source_location::current())
{
   return a; // Line 3
}

void f(const std::experimental::source_location& a = there()) // Line 6
{
   std::cout << a.line() << std::endl;
}

I expected the following output:

13
14

but I got:

13
6

Why does the code behave that way? Is there a way to make it behave as expected without using a pre-processor macro?

Update: The second variant with 'constexpr' or 'inline' instead of 'consteval' works fine with most recent GCC. So the remaining question is: Why doesn't 'consteval' work as well? I was told this isn't a standard issue but an implementation topic so I will re-tag this question as GCC.


Solution

  • I turned out second variant is correct. On current GCC 12 it works fine.