C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Supporting f-strings in C++: draft-R1

From: Hadriel Kaplan <hkaplan_at_[hidden]>
Date: Mon, 16 Oct 2023 18:14:30 +0000
> From: coshvji cujmlqef <oyzawqgcfc_at_[hidden]>

> I do not see any reason why you need x-string.
> if you have a proper concat function, then you just do concat(f"abc={abc}, def={def}"). Then it will translate concat("abc=",abc,", def=",def); Which is exactly the implementation which fast_io provides.
> fast_io/examples/0031.concat/concat_code_cvt.cc at master · cppfastio/fast_io (github.com)

I think you misunderstand. The x-string is the part that actually does all the work: takes a string, extracts expressions from it, and replaces it with a new format-string and the expressions unpacked.

An f-string is just a shorthand convenience for a std::format(X"...").

---

Are you saying you want the x-string to instead split things apart into separate substrings and expressions the way you show above? That's a different thing altogether.

The concat() function you showed above is not at all the same semantics as a format-string.

You’d lose the actual formatting information - the "format-spec" aspect: the ability to specify how to display things, individually, with even custom formatting-codes for custom types.

In other words, it's not only this that's useful:

    f"abc={abc}, def={def}"

It's this:

    f"abc={abc :0.3x}, def={def :HH:MM:SS}"

Where "abc" and "def" could be some class/struct types I wrote, or some third-party library's types, or standard types.

---

The cool thing with an x-string is that you can STILL do what you're asking for.

Because this is all happening at compile-time. So you can take the output of the x-string's conversion (the unpacked new format-string and arguments), and parse that new format-string _again_ yourself - at compile time. And thus you can create a data-structure similar to std:: format_string<> and also at compile-time, but one that pre-determines and saves the offsets in the string to use for the portions of the format-string that you care about (let's call it a "string_snippets").

So then something like this:

    template <class... Args>
    auto concat_fmt(string_snippets<Args...> snippets, Args&&... args);

Could be used like this:

    concat_fmt(X"abc={abc}, def={def}")

And it would do the same thing as concat(), with the same runtime performance, and with the same lack of any real formatting if you don't want it. Though compile-times would be longer.

-hadriel



Juniper Public

Received on 2023-10-16 18:14:35