C++20 std::source_location 在自由函数和模板函数之间产生不同的列号

康桓瑋*_*康桓瑋 6 c++ function-templates c++20 std-source-location

考虑模板函数g()和自由函数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();
}
Run Code Online (Sandbox Code Playgroud)

GCC-trunk编译得到以下输出:

43
44
Run Code Online (Sandbox Code Playgroud)

为什么g()f()产生不同的结果?我希望结果是一样的。为什么在模板实例化过程中单位偏移消失了?

康桓瑋*_*康桓瑋 3

我向 GCC Bugzilla 提交了PR 99672。Jakub Jelinek(GCC 贡献者之一)回复我:

我认为该标准没有指定任何关于列应该是什么的信息,因此使用不同的列并不违反标准。

但他还是打了补丁来修复它。