0

I was trying to figure out ways to use std::source_location::current(), when I stumbled upon this particular answer for a thread.

I tried running the code on godbolt with x86-64 gcc 13.1 and -O3 -std=c++20 -Wall -Wextra -Wpedantic, but it doesn't even compile.

Code:

#include <format>
#include <iostream>
#include <source_location>

template<typename... Targs, auto location = std::source_location::current()>
auto tprintf(char const* format, Targs const&... args) -> void {
    std::cout
        << std::format("{}:{}: ", location.file_name(), location.line())
        << std::format(format, args...);
}

auto main() -> int {
    tprintf("hello {}", "world"); // prints "example.cpp:12: hello world"
}

Am I doing something wrong? If not, why was this even accepted as an answer if it doesn't compile?

spaL
  • 604
  • 7
  • 21
  • Please provide link to godbolt (after all if you mentioned it, it should be logical to share a link to not working example). – Marek R May 30 '23 at 08:41
  • `it doesn't even compile.` What is the error? – KamilCuk May 30 '23 at 08:42
  • Link: https://gcc.godbolt.org/z/xqb5xr5eG – spaL May 30 '23 at 08:42
  • error: 'std::source_location' is not a valid type for a template non-type parameter because it is not structural – spaL May 30 '23 at 08:44
  • 2
    I've checked linked answer. Author provided [own replacement of `std::source_location`](https://godbolt.org/z/jG6atf). Current version of `std::source_location` in `gcc` version apparently can't be used as template parameter. So your code simply doesn't work with recent compiler. Maybe it will in the future. – Marek R May 30 '23 at 08:53

0 Answers0