Date: Sat, 14 Oct 2023 20:55:32 +0000
> From: Barry Revzin <barry.revzin_at_[hidden]>
> I think this approach is kind of a non-starter. We can't have f"x={x}" just mean std::format("x={}", x) for an important reason.
> This means that std::print(f"x={x}") doesn't and can't work - which is the sort of thing that seems important to support - having to write std::print("{}", f"x={x}") is... less than ideal.
> More generally, the issue is that there are a lot of uses of the format API that are not literally std::format(). In the standard we have std::print(), std::format_to(), etc. It would be really nice if those worked with f-strings as well.
I explicitly covered that topic in section 5 of the draft...
We only need to add another overload to std::print(), std::println(), and format_to() - one that accepts a std::string_view or std::string - to make these work:
std::print(F"hello C++{20 + 6}");
std::println(F"hello C++{13 * 2}");
std::format_to(std::back_inserter(buffer), F"hello C++{26}");
The first two functions might not be as efficient as using a separate format_string and args, because an intermediate std::string is created which might be avoidable otherwise.
But they're already writing to stdout, so an extra temporary string seems rather meaningless? At least I think it's reasonable tradeoff for convenience.
Likewise, this should work without spdlog being changed at all:
spdlog::info(f"x={x}");
...because *anything* that accepts a std::string rvalue should work - because ultimately the f-string resolves to the std::string returned by std::format().
You can almost ignore the fact it uses std::format(), and instead just think of an f-string as a std::string&&. Or perhaps like a new std::to_string(fmt) function. The fact it uses std::format() to create the string is arguably an implementation detail, in some sense.
---
As an aside: it's actually odd to me that std::print/println do NOT already accept a single std::string_view arg today. They do if the string/string_view is constexpr (via implicit conversion), but not a runtime one. I mean I know why it's happening, but it seems wrong to me - an unexpected surprise as a user, and an unnecessary restriction for those functions.
---
Thanks for the review!
-hadriel
Juniper Public
> I think this approach is kind of a non-starter. We can't have f"x={x}" just mean std::format("x={}", x) for an important reason.
> This means that std::print(f"x={x}") doesn't and can't work - which is the sort of thing that seems important to support - having to write std::print("{}", f"x={x}") is... less than ideal.
> More generally, the issue is that there are a lot of uses of the format API that are not literally std::format(). In the standard we have std::print(), std::format_to(), etc. It would be really nice if those worked with f-strings as well.
I explicitly covered that topic in section 5 of the draft...
We only need to add another overload to std::print(), std::println(), and format_to() - one that accepts a std::string_view or std::string - to make these work:
std::print(F"hello C++{20 + 6}");
std::println(F"hello C++{13 * 2}");
std::format_to(std::back_inserter(buffer), F"hello C++{26}");
The first two functions might not be as efficient as using a separate format_string and args, because an intermediate std::string is created which might be avoidable otherwise.
But they're already writing to stdout, so an extra temporary string seems rather meaningless? At least I think it's reasonable tradeoff for convenience.
Likewise, this should work without spdlog being changed at all:
spdlog::info(f"x={x}");
...because *anything* that accepts a std::string rvalue should work - because ultimately the f-string resolves to the std::string returned by std::format().
You can almost ignore the fact it uses std::format(), and instead just think of an f-string as a std::string&&. Or perhaps like a new std::to_string(fmt) function. The fact it uses std::format() to create the string is arguably an implementation detail, in some sense.
---
As an aside: it's actually odd to me that std::print/println do NOT already accept a single std::string_view arg today. They do if the string/string_view is constexpr (via implicit conversion), but not a runtime one. I mean I know why it's happening, but it seems wrong to me - an unexpected surprise as a user, and an unnecessary restriction for those functions.
---
Thanks for the review!
-hadriel
Juniper Public
Received on 2023-10-14 20:55:39