6

Consider the template function g() and free function f():

#include <iostream>
#include <source_location>

auto g(auto...) {
std::cout << std::source_location::current().column() << "\n";
}

auto f() {
std::cout << std::source_location::current().column() << "\n";
}

int main() {
g();
f();
}

Compiled with GCC-trunk get following output:

43
44

Why g() and f() yield different results? I expect the results are the same. Why a unit offset disappeared during the instantiation of the template?

康桓瑋
  • 33,481
  • 5
  • 40
  • 90
  • 5
    I would say bug, especially as feature is only in trunk so young... – Jarod42 Mar 19 '21 at 14:27
  • 1
    If I had to guess, I would say this is likely a compiler bug. Handling column location is new to standard-C++; previous versions only had a `__LINE__` and not an equivalent `__COLUMN__`. Because of this, I wouldn't be surprised if some compilers like `gcc` suffer growing pains as they have to adapt their existing compiler code to now do proper detection. – Human-Compiler Mar 19 '21 at 14:30
  • Ok I will report this, thanks. – 康桓瑋 Mar 19 '21 at 15:02
  • 1
    @康桓瑋 If you report it, please consider posting the report as an answer for visibility! I'm sure other people will encounter this eventually as well. Plus I wouldn't mind seeing their follow-up – Human-Compiler Mar 19 '21 at 15:52

1 Answers1

3

I file a PR 99672 to GCC Bugzilla. Jakub Jelinek (one of the GCC contributor) reply me:

I think the standard doesn't specify anything about what exactly the column should be, so using different columns isn't standard violation.

but he still did a patch to fix it.

康桓瑋
  • 33,481
  • 5
  • 40
  • 90