C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Newlines in F-strings

From: Chris Gary <cgary512_at_[hidden]>
Date: Tue, 24 Oct 2023 17:53:18 -0600
Also, expanding directly to std::format would limit this to cases where
only the appropriate std::string variant can be used.
While it may seem inconvenient, being able to do something like this:

FormatQString( F"Values of {gamma} may give rise to {condition}" )
to
FormatQString( "Values of {} may give rise to {}", gamma, condition );

Without having to construct with an std::string would eliminate a locally
small, but at large significant, amount of overhead.
Instead of expanding to a call to std::format, the transformation could be
entirely symbolic, just producing a format string and a list of arguments.

On Tue, Oct 24, 2023 at 5:41 PM Chris Gary <cgary512_at_[hidden]> wrote:

> Chances are I missed this earlier, but in the case of a raw literal, how
> is a literal "{" or "}" given?
> It looks as though that could be the only special case here, where
> escaping would just be "\{" or "\}".
> Am I correct here?
>
> On Tue, Oct 24, 2023 at 5:32 PM Hadriel Kaplan via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> Hi,
>>
>> While talking with Bengt offline about various use-cases for f-strings,
>> he brought up one that I'm not sure about.
>>
>> Should this compile? (note the 'R' for raw-string):
>>
>> FR"(name={
>> get_name()
>> }, time={
>> std::system_clock::now()
>> :%H:%M:%S
>> })";
>>
>> Right now, in the next rev of the draft I'm working on, the above would
>> treated identically as this non-raw f-string:
>>
>> F"name={\nget_name()\n}, time={\nstd::system_clock::now():%H:%M:%S\n}";
>>
>>
>> And it would interpolate to this:
>>
>> std::format("name={}, time={:%H:%M:%S\n}",\n get_name()\n,\n
>> std::system_clock::now());
>>
>> Note that there are multiple sets of the two characters "\" and "n"
>> within the extracted arguments (not escaped newlines).
>>
>> So it will fail to compile due to those characters.
>>
>> ---
>>
>> If you think it should compile, what should the format-string be after
>> interpolation?:
>>
>> 1. "name={}, time={:%H:%M:%S\n}"
>> or
>> 2. "name={}, time={:%H:%M:%S}"
>>
>> And if (2), how do we know the programmer didn't want the newline? It is
>> perfectly legal to have one there for chrono formatting, I believe.
>>
>> ---
>>
>> Personally I think it's ok for this scenario to fail to compile, but I
>> also think it's ok to succeed instead.
>>
>> The programmer can always do this instead anyway, if they want to split
>> things up:
>>
>> F"name={ get_name() }, "
>> F"time={ std::system_clock::now():%H:%M:%S} )";
>>
>> -hadriel
>>
>>
>>
>> Juniper Public
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
>

Received on 2023-10-24 23:53:31