#include#include void logger(const std::string& message, const std::experimental::source_location& location = std::experimental::source_location::current()) { std::cout << "[" << location.file_name() << " " << location.function_name() << " " << location.line() << "] " << message << std::endl; } int main() { logger("Hello world!"); return 0; }
#includeThis example shows how to manually call the `current()` function of the SourceLocation class to get information about the current location in the code. In this case, the location of the function `myFunction()` is printed to the console. The package/library for this code is the C++20 standard library.#include struct MyClass { void myFunction() { auto loc = std::source_location::current(); std::cout << "In function " << loc.function_name() << " at line " << loc.line() << " in file " << loc.file_name() << std::endl; } }; int main() { MyClass myInstance; myInstance.myFunction(); return 0; }