News for hackers. Stuff that matters.
Re:std::source_location Is Broken by elteto on Thursday November 16, 2023 @02:21PM (#38294077)
> You need to know the size at compile time. std::source_location::file_name() gives you a const char* – you don’t know the size of the string. And because it has to be a function argument, it can’t be constexpr.

At least this part is not entirely correct, the following compiles in C++20:

  consteval std::string_view filename(const std::source_location &loc = std::source_location::current()) {
    return loc.file_name();
  }
See [0]. I don't know how to go from here to a type, as the author wants, but I'll venture that there's some ungodly template metaprogramming hack that will get you there.

[0] https://godbolt.org/z/K6ejTr9cn

Re:std::source_location Is Broken by lionkor on Thursday November 16, 2023 @03:27PM (#38295001)
you can also do compile time strlen(), if you are ok with goofy variadic templates
Re:std::source_location Is Broken by UebVar on Thursday November 16, 2023 @05:14PM (#38296356)
Implementing a constexpr strlen() is trivial and I looks just like it's from your ancient C textbook, save for the "constexpr" keyword. No goofyness involved.

Or you use what the C++ standard library has to offer.

std::string_view(ptr).length(); or std::string(ptr).length(); or std::char_traits<char>::length(ptr); all work at compile time.

Re:std::source_location Is Broken by gpderetta on Thursday November 16, 2023 @05:36PM (#38296621)
So, one issue is that source_location is not a valid non-type template parameter as it is not a structural type. But you can trivially make one yourself if you chose an upper bound on the filename (and function name) size.

The second issue is that it seems that default values for non-type template parameters are not evaluated at the instantiation location, but at the definition location, so you need to make the template parameter explicit. This is the best I could come up with:

   log<o>("hello", "world")  // at example.cpp:30
   
which prints[1]:

   app/example.cpp:30: hello world
[1] https://godbolt.org/z/z1eGc6cM4
Re:std::source_location Is Broken by elteto on Thursday November 16, 2023 @07:52PM (#38297830)
Very, very nice!

Could even reuse the explicit template parameter for the verbosity level, like:

  log<I>(…);  // info
  log<D>(…);  // debug
Re:std::source_location Is Broken by gpderetta on Friday November 17, 2023 @02:27AM (#38300576)
Ooh, excellent Idea! Make it a feature from a limitation!