1

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?

5
  • Please provide link to godbolt (after all if you mentioned it, it should be logical to share a link to not working example). Commented May 30, 2023 at 8:41
  • it doesn't even compile. What is the error? Commented May 30, 2023 at 8:42
  • Link: gcc.godbolt.org/z/xqb5xr5eG Commented May 30, 2023 at 8:42
  • error: 'std::source_location' is not a valid type for a template non-type parameter because it is not structural Commented May 30, 2023 at 8:44
  • 2
    I've checked linked answer. Author provided own replacement of std::source_location. 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. Commented May 30, 2023 at 8:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.