C++ Logo

std-proposals

Advanced search

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

From: Hadriel Kaplan <hkaplan_at_[hidden]>
Date: Fri, 13 Oct 2023 14:01:10 +0000
From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>

> pt., 13 paź 2023 o 14:24 Hadriel Kaplan <mailto:hkaplan_at_juniper.net> napisał(a):

> > Does the user need to provide a templated literal operator, such as:
> > template <class... Args>
> > std::string operator ""_w(const char*, size_t, Args&&...) { ... }
> > The above isn't currently legal, and I'm not sure it can be made legal given we already have the string literal operator non-type template from C++20, but let's assume it can - would everyone need to implement such a literal operator?

> Yes, this is the whole point of this.

OK, but in my opinion that's the point of P1819, not the point of a feature called "f-strings".

By definition, the "f" in "f-string" stands for "format", as does the "F" prefix. It's entire purpose in life is to provide a shorthand for format(), that is both less verbose and clearer to see the usage of arguments in.

And a significant attraction of f-strings is their simplicity. You shouldn't have to write additional code to use f-strings. It should be trivial to use, even for novices, and even in a cppreference.com code-example or on godbolt or whatever.

What you're talking about is something else: custom transformation or interpolation or whatever. Let's call it "x-string". It could use a `X"{arg}"_w` syntax to achieve it, and follow what you suggested. And obviously some of the compiler's conversion logic to handle f-strings could be re-used for x-strings.

X-strings would definitely be an interesting/flexible mechanism. But in my opinion it's not really an f-string. It does not format anything, and it's both harder to implement as well as harder to use.

-hadriel
p.s. I forgot to say in the last email: thank you for the feedback!



Juniper Business Use Only

Received on 2023-10-13 14:01:25