TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

F-strings for C++26 proposal [pdf]

183 pointsby HeliumHydride3 months ago

22 comments

edflsafoiewq3 months ago
So the f-string literal produces a basic_formatted_string, which is basically a reified argument list for std::format, instead of a basic_string. This allows eg. println to be overloaded to operate on basic_formatted_string without allocating an intermediate string<p><pre><code> std::println(&quot;Center is: {}&quot;, getCenter()); std::println(f&quot;Center is: {getCenter()}&quot;); &#x2F;&#x2F; same thing, no basic_string allocated </code></pre> In exchange we have the following problems<p><pre><code> &#x2F;&#x2F; f-strings have unexpected type when using auto or type deduction. &#x2F;&#x2F; basic_string is expected here, but we get basic_formatted_string. &#x2F;&#x2F; This is especially bad because basic_formatted_string can contain &#x2F;&#x2F; dangling references. auto s = f&quot;Center is: {getCenter()}&quot;; &#x2F;&#x2F; f-strings won&#x27;t work in places where providing a string currently &#x2F;&#x2F; works by using implicit conversion. For example, filesystem methods &#x2F;&#x2F; take paths. Providing a string is okay, since it will be implicitly &#x2F;&#x2F; converted to a path, but an f-string would require two implicit &#x2F;&#x2F; conversions, first to a string, then to path. std::filesystem::exists(f&quot;file{n}.dat&quot;); &#x2F;&#x2F; error, no matching overload </code></pre> There are two other proposals to fix these problems.
评论 #42915835 未加载
评论 #42915733 未加载
评论 #42913399 未加载
评论 #42915722 未加载
评论 #42916027 未加载
评论 #42930184 未加载
mkoubaa3 months ago
Tangent: this sort of thing can be implemented without any change to libc++ (the runtime). Updates to compiler versions are sometimes postponed by users with big codebases that treat a libc++ change as something major.<p>Why don&#x27;t we see gcc or clang or msvc back porting stuff like this to an older version with a sort of future tag. It&#x27;s normal to see __future__ in the python ecosystem, for instance.
评论 #42915561 未加载
评论 #42913170 未加载
beeforpork3 months ago
It would be nice to take care to allow the use of GNU gettext() or any other convenient translation tool.<p>Recap: _(&quot;foo %s&quot;) macroexpands to gettext(&quot;foo %s&quot;), then &quot;foo %s&quot; is extracted to a lexicon of strings by an external tool, which can be translated and compiled into .po files, which are loaded at runtime so gettext() can use a translated string based on $LC_MESSAGES. (And there is also _N(..) for correct plural handling.)<p>To do this with f-strings, _(f&quot;foo {name()}&quot;) (which is a bit ugly...) needs to translate to make_formatted_string(_(&quot;foo {}&quot;), name()) -- note that the _(...) needs to be called before calling make_formatted_string, to be able to return a translated string.<p>I would wish for a proposal for f-strings to consider translating strings, because we live in a world with many languages. And maybe cite gettext as a convenient method, and think about what could be done. Or point to a better tool. Or state: &#x27;in that case, f-strings cannot be used&#x27;.
评论 #42919881 未加载
评论 #42917651 未加载
mixmastamyk3 months ago
So, the f-string in Python is &quot;spelled&quot; that way because another leading character was the only ASCII syntax left for such a thing. It&#x27;s odd that PRQL and now potentially C++ might copy it. In the PRQL case it was a new thing so they could have chosen anything, double quotes (like shell interpolation) or even backticks, that seem to make more sense.<p>Also the f- prefix was supposed to be short for format and pronounced that way. But &quot;eff&quot; caught on and now devs the world over are calling them &quot;eff strings&quot; ... funny. :-D
评论 #42916430 未加载
评论 #42916225 未加载
评论 #42927616 未加载
评论 #42917178 未加载
vitaut3 months ago
This proposal is not targeting C++26 and there is a new revision of it: <a href="https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg21&#x2F;docs&#x2F;papers&#x2F;2025&#x2F;p3412r1.pdf" rel="nofollow">https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg21&#x2F;docs&#x2F;papers&#x2F;2025&#x2F;p34...</a>
amluto3 months ago
This links to a “decays_to” proposal:<p><a href="https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg21&#x2F;docs&#x2F;papers&#x2F;2024&#x2F;p3398r0.pdf" rel="nofollow">https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg21&#x2F;docs&#x2F;papers&#x2F;2024&#x2F;p33...</a><p>And observes that this additional feature is needed to avoid dangling references. And, as a long time C++ programmer, this illustrates one of the things I dislike most about C++. In most languages, if you make a little mistake involving mixing up something that references something else with something that contains a copy, you end up with potential overhead or maybe accidental mutation. In Rust, you get a compiler error. In C++, you get use-after-free, and the code often even seems to work!<p>So now we expect people to type:<p><pre><code> auto s = f&quot;{foo}&quot;; </code></pre> And those people expect s to act like a string. But the designers (reasonably!) do not want f to unconditionally produce an actual std::string for efficiency reasons, so there’s a proposal to allow f to produce a reference-like type (that’s a class value, not actually a reference), but for s to actually be std::string.<p>But, of course, more advanced users might know what they’re doing and want to bypass this hack, so:<p><pre><code> explicit auto s = f&quot;{foo}&quot;; </code></pre> Does what they programmer actually typed: s captures foo by reference.<p>What could possibly go wrong?<p>(Rust IMO gets this exactly right: shared xor mutable means plus disallowing code that would be undefined behavior means that the cases like this where the code might do the wrong thing <i>don’t compile</i>. Critically, none of this actually strictly requires Rust’s approach to memory management, although a GC’d version might end up with (deterministic) runtime errors instead unless some extra work is done to have stronger static checking. And I think other languages should learn from this.)
评论 #42913767 未加载
评论 #42913463 未加载
dgfitz3 months ago
Somehow I manage to get by just fine with c++11. I have refactored more than a few codebases that use 17 or greater.<p>Strangely, the codebase became more maintainable afterwards.
评论 #42915454 未加载
评论 #42914230 未加载
Laiho3 months ago
Is it a coincidence that all these quality life things start to pop up after C++ is facing real competition for the first time? Seems a bit odd to add print after using std::out for 30 years.
评论 #42915888 未加载
feverzsj3 months ago
Like the compile time isn&#x27;t long enough. They should just let the compiler do the job like Wformat=2 and skip any preprocess and constexpr function.
评论 #42915573 未加载
puffybuf3 months ago
I&#x27;m pretty sure boost::format can do this, though not inline in the string. Do we really need more complexity in cpp? isn&#x27;t it complex enough?
评论 #42913115 未加载
评论 #42913126 未加载
评论 #42913154 未加载
评论 #42913129 未加载
biofox3 months ago
Making any changes to the core language is a sensitive thing as it inevitably imposes new demands on compilers, a learning curve for all users of the language, and risks breaking compatibility and introducing unforeseen issues that will need to be fixed with future changes to the language.<p>Personally, I&#x27;d much prefer a smaller and more stable language.
评论 #42915960 未加载
neonsunset3 months ago
Reinventing C#’s FormattableString and interpolated string handlers :)
评论 #42915994 未加载
dingxiong3 months ago
I am tired of PDFs. They should have a dedicated website for presenting C++ proposals so everyone can comment and discuss. Reading Github issues are more enjoyable than reading PDFs.
andyg_blog3 months ago
I agree that we should have safe-by-default &quot;decay&quot; behavior to a plain ol std::string, but I&#x27;m also picking up that many aren&#x27;t certain it&#x27;s a useful syntactic sugar in top of the fmt lib? Many other languages have this same syntax and it quickly becomes your go-to way to concatenate variables into a string. Even if it didn&#x27;t handle utf-8 out of the box, so what? The amount of utility is still worth it.
Yoric3 months ago
That&#x27;s funny. We introduced essentially this features in OCaml Batteries Included ~20 years ago. This brings back memories :)
robertlagrant3 months ago
I&#x27;m hoping the next version gets the walrus operator.
nickysielicki3 months ago
Reflection is going to change things so much.
评论 #42927649 未加载
ggm3 months ago
I&#x27;m going to make an asinine prediction. We will be exploring F-strings in future languages in 100 years time, encountering the same problems and questions.<p>I still use printf semantics in Python3 despite trying to get with the program for symbolic string&#x2F;template logic. I don&#x27;t need to be told it&#x27;s better, I need some Philip-K-Dick level brain re-wiring not to reach for<p><pre><code> &quot;%d things I hate about f-strings\n&quot; % (int(many())) </code></pre> modes of thinking.
评论 #42916369 未加载
评论 #42916094 未加载
otteromkram3 months ago
Decline based on usage of pascalCase in the first example. How did that even happen?
评论 #42915717 未加载
efitz3 months ago
When I saw the title I thought “F-strings” might be some novel variant of P—strings. I was disappointed that this is just about formatting. I really would prefer safer string handling in modern C&#x2F;++
boxed3 months ago
Jesus. This is such a bad idea. Don&#x27;t repeat the mistakes of Python. Look at what Swift does and make a SANE system ffs.
评论 #42916539 未加载
评论 #42916255 未加载
评论 #42916177 未加载
alterom3 months ago
Yeah, I really missed ubiquitous C preprocessor macros in C++, so let&#x27;s bring them back, but now inside string literals. Sweet.<p>Seriously, I just keep being amazed that people are running with the idea of having a full-blown untyped and unchecked <i>formatting mini language</i> (that&#x27;s what libfmt, which became C++20 format, literally calls it) inside <i>string literals</i> — i.e., the part of the source code that you&#x27;re specifically telling the compiler to <i>not treat as code</i>.<p>Think about it for a minute.
评论 #42914989 未加载
评论 #42915039 未加载
评论 #42915459 未加载