C++ Logo

std-proposals

Advanced search

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

From: Henry Miller <hank_at_[hidden]>
Date: Wed, 28 Dec 2022 02:16:37 -0600
Your second objection to format is a basic requirement of all string systems, and so any string library, in any programming language that doesn't feature that is wrong. It is critical that all programmers get used to the idea their strings may need to be translated to some foreign language. Different languages feature different word order and so the order the variables appear in the output must be flexible.

While is seems likely that any string that gets translated will use the strings provided by their UI toolkit and not std, we should still encourage the mindset in young programmers that variables should not be in the same order as the output.



-- 
  Henry Miller
  hank_at_[hidden]
On Wed, Dec 28, 2022, at 00:59, Yuri via Std-Proposals wrote:
> **********************************
> ** 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 08:17:04