C++ Logo

std-proposals

Advanced search

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

From: Hadriel Kaplan <hkaplan_at_[hidden]>
Date: Sat, 14 Oct 2023 21:29:38 +0000
> From: Std-Proposals <std-proposals-bounces_at_[hidden].org> on behalf of Tom Honermann via Std-Proposals <std-proposals_at_lists.isocpp.org>

> The proposal introduces a parsing ambiguity that will need to be
addressed in some way:
> F"{b?1:0}" // expression `b?1:0` with no format specifier?
> // or ill-formed expression `b?1` with a `:0` format
> specifier?

Right, it has that issue. I mentioned it in section-8 "Error Cases", but in the next rev I'm going to split that issue out into its own topic.

I plan on proposing to just do exactly what python does: require the user to wrap the expression in parentheses if they need a colon:

    F"(b?1:0)"; // no format-specifier
    F"(b?1:0):s"; // with format-specifier

I also had to change the parsing rules a bit: the _first_ standalone colon is the delimiter, instead of the last one.

Because that's what std::format() does, so that things like std::chrono can work:

    F"The time is { chrono::system_clock::now() :HH:MM:SS}";

-hadriel



Juniper Public

Received on 2023-10-14 21:29:43