C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Proposal to add f-strings to C++ as an improvement of string formatting

From: Guillaume Racicot <gracicot42_at_[hidden]>
Date: Wed, 28 Dec 2022 11:02:36 -0300
There was a long time ago a proposal for format string, I can't remember
the number. In any case, the old one should be rebased to be in line with
fmt and if there's a new one it should also be based on fmt formatting.

Also, if you truly propose the compiler replace f"aaa {expr} bbb" with "aaa
" << expr << " bbb" it means that the following code would not work:

void g(std::string);
g(f"aaa{expr}bbb");

This is because the bit shift operator will be called, but don't make sense
in this context.

If we based the string expressions on fmt, then the following should work.

You might also have dangerous combinations if you use stream operators
blindly:

int a = 1;
int b = 4;

f"{a}{b}" // equivalent to "16"??

There is also a couple of important questions to be answered:

 - What is the type of f""? Depending on the answer, using those can be as
fast as format, slower or faster.
 - When is the expression inside the {} evaluated? Depending on the answer,
you might there might have performance implications or a footgun with
temporaries and scopes.
 - How are the format options sent to a f string?
 - Does using this language feature will require the inclusion of a
specific header? The answer will probably depend on what you answered on
the other questions.

Em qua., 28 de dez. de 2022 03:59, Yuri via Std-Proposals <
std-proposals_at_[hidden]> escreveu:

> **********************************
> ** Problem that f-strings solve **
> **********************************
>
> String formatting in C++ requires more characters than minimally necessary.
>
> There are two major ways to format complex strings in C++:
> 1. using operator<<, for example:
> std::cout << "threshold is " << threshold << ", time is now " <<
> time() << std::endl; (1)
> 2. using std::format, for example:
> std::cout << std::format("threshold is {}, time is now {}",
> threshold, time()) << std::endl; (2)
>
> Both ways have problems: they require more characters than minimally
> necessary to perform the task of formatting. The second way also separates
> variables and their print locations, which makes it more error prone.
>
> It's common to need to do a lot of complex message printing for programs,
> when various values or expressions need to be incorporated into the
> message.
> C++ code in these cases is less expressive than it can be.
>
> **********************
> ** Proposed feature **
> **********************
>
> Introduce a special kind of string in C++: f-string.
>
> f-string begins with the 'f' character (f"string" or Lf"string"). When an
> f-string is encountered it is interpreted in the same way as regular
> strings
> with this exception:
>
> > The '{' character is assumed to begin an rvalue expression. Compiler
> would
> > change context after '{' and would begin to parse the rvalue, expecting
> > to find the closing '}' after the rvalue ends. The closing '}' would
> restore
> > the string context. In order for '{' and '}' characters to appear in
> > an f-string they should be escaped, \{ and \}.
>
> Compiler would replace all "...{<rvalue>}..." tokens in f-strings with
> "..." << <rvalue> << "...", allowing them to be further processed in the
> same
> way as an operator<<-based print construct.
>
> This way forming complex strings in C++ would become simpler. The above
> example
> would look like:
> std::cout << "threshold is {threshold}, time is now {time()}" <<
> std::endl; (3)
> This is more readable and intuitive than both (1) and (2).
>
> *****************
> ** Limitations **
> *****************
>
> (1) rvalues should be printable with operator<<.
> (2) for simplicity macros wouldn't be allowed in rvalues embedded in
> f-strings.
>
> *************************************
> ** Affected elements of toolchains **
> *************************************
>
> * C++ compiler
> * Syntax highlighters in editors and other software
>
> **************
> ** Benefits **
> **************
>
> f-strings would simplify string formatting and allow to make string
> formatting
> code as simple as possible. This would make printing code less error
> prone, and
> would make it much easier to read.
>
> *****************************
> ** What inspired this idea **
> *****************************
>
> (1) While writing a lot of C++ code with many complex messages
> incorporating
> a lot of various values I was thinking that it got to be a shorter and
> easier way of doing this.
> (2) While seeing f-strings in Python recently I realized that C++ can also
> greatly benefit from this feature.
>
> *************
> ** Example **
> *************
>
> This code would be legal C++ code with f-strings:
>
> #include <chrono>
> #include <ctime>
> #include <iostream>
> #include <pwd.h>
> #include <unistd.h>
>
> using namespace std;
>
> int main() {
> auto now =
> chrono::system_clock::to_time_t(chrono::system_clock::now());
> cout << "Hello {getpwuid(geteuid())->pw_name}"
> ", the time now is {ctime(&now)}." << endl;
> }
>
>
>
> Thank you,
> Yuri Victorovich
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2022-12-28 14:02:51