In C++20 a new feature was added to get the source location information: https://en.cppreference.com/w/cpp/utility/source_location
Here is a slightly modified example from that page, where an addition immediate function loc
is used to get the source location:
#include <iostream>
#include <string_view>
#include <source_location>
consteval auto loc(std::source_location x = std::source_location::current() ) { return x; }
void log(const std::string_view message,
const std::source_location location = loc()) {
std::cout << "file: "
<< location.file_name() << "("
<< location.line() << ":"
<< location.column() << ") `"
<< location.function_name() << "`: "
<< message << '\n';
}
template <typename T> void fun(T x) { log(x); }
int main(int, char*[]) {
log("Hello world!");
fun("Hello C++20!");
}
In the latest MSVC 2019 it prints as in the original example from cppreference.com:
file: main.cpp(25:5) `main`: Hello world!
file: main.cpp(20:5) `fun`: Hello C++20!
But in GCC the same line is indicated twice in the output:
file: /app/example.cpp(8:51) ``: Hello world!
file: /app/example.cpp(8:51) ``: Hello C++20!
demo: https://gcc.godbolt.org/z/nqE4cr9d4
Which of the compilers is right here?
And if define loc
function not as immediate one:
auto loc(std::source_location x = std::source_location::current() ) { return x; }
then the output of GCC changes and resembles the original example:
file: /app/example.cpp(20:8) `int main(int, char**)`: Hello world!
file: /app/example.cpp(17:42) `void fun(T) [with T = const char*]`: Hello C++20!
While MSVC refuses to compile it with the error:
error C7595: 'std::source_location::current': call to immediate function is not a constant expression
demo: https://gcc.godbolt.org/z/vorW4f9ax
Please also suggest, which compiler is right in not-immediate case as well?
loc
does. Declarelog
function as intended usingstd::source_location::current()
as the default value. – Phillis