C++20 std::source_location

This is a compile time utility, which replaces macros like FILE and LINE.

I’m partial to using functions over macros, and this compile time utility looks nice:

#include <iostream>
#include <source_location>

int main(int, char**)
{
    auto location = std::source_location::current();

    auto file_name  = location.file_name();
    auto line       = location.line();
    auto column     = location.column();
    auto func_name  = location.function_name();

    std::cout << file_name << "\n";
    return 0;
}

This is weak example of what you can do with the above class, but you get the idea.

Leave a Reply

Your email address will not be published. Required fields are marked *