Skip to content Skip to footer

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

11 Comments

  • Post Author
    puffybuf
    Posted February 2, 2025 at 11:42 pm

    I'm pretty sure boost::format can do this, though not inline in the string. Do we really need more complexity in cpp? isn't it complex enough?

  • Post Author
    mkoubaa
    Posted February 2, 2025 at 11:45 pm

    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.

    Why don't we see gcc or clang or msvc back porting stuff like this to an older version with a sort of future tag. It's normal to see __future__ in the python ecosystem, for instance.

  • Post Author
    edflsafoiewq
    Posted February 3, 2025 at 12:15 am

    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

      std::println("Center is: {}", getCenter());
      std::println(f"Center is: {getCenter()}");  // same thing, no basic_string allocated
    

    In exchange we have the following problems

      // f-strings have unexpected type when using auto or type deduction.
      // basic_string is expected here, but we get basic_formatted_string.
      // This is especially bad because basic_formatted_string can contain
      // dangling references.
      auto s = f"Center is: {getCenter()}";
    
      // f-strings won't work in places where providing a string currently
      // works by using implicit conversion. For example, filesystem methods
      // take paths. Providing a string is okay, since it will be implicitly
      // converted to a path, but an f-string would require two implicit
      // conversions, first to a string, then to path.
      std::filesystem::exists(f"file{n}.dat");  // error, no matching overload
    

    There are two other proposals to fix these problems.

  • Post Author
    amluto
    Posted February 3, 2025 at 12:16 am

    This links to a “decays_to” proposal:

    https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p33…

    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!

    So now we expect people to type:

        auto s = f"{foo}";
    

    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.

    But, of course, more advanced users might know what they’re doing and want to bypass this hack, so:

        explicit auto s = f"{foo}";
    

    Does what they programmer actually typed: s captures foo by reference.

    What could possibly go wrong?

    (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 don’t compile. 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.)

  • Post Author
    dgfitz
    Posted February 3, 2025 at 1:01 am

    Somehow I manage to get by just fine with c++11. I have refactored more than a few codebases that use 17 or greater.

    Strangely, the codebase became more maintainable afterwards.

  • Post Author
    efitz
    Posted February 3, 2025 at 1:22 am

    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/++

  • Post Author
    andyg_blog
    Posted February 3, 2025 at 1:34 am

    I agree that we should have safe-by-default "decay" behavior to a plain ol std::string, but I'm also picking up that many aren't certain it'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't handle utf-8 out of the box, so what? The amount of utility is still worth it.

  • Post Author
    mixmastamyk
    Posted February 3, 2025 at 2:19 am

    So, the f-string in Python is "spelled" that way because another leading character was the only ASCII syntax left for such a thing. It'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.

    Also the f- prefix was supposed to be short for format and pronounced that way. But "eff" caught on and now devs the world over are calling them "eff strings" … funny. :-D

  • Post Author
    neonsunset
    Posted February 3, 2025 at 2:39 am

    Reinventing C#’s FormattableString and interpolated string handlers :)

  • Post Author
    ggm
    Posted February 3, 2025 at 3:37 am

    I'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.

    I still use printf semantics in Python3 despite trying to get with the program for symbolic string/template logic. I don't need to be told it's better, I need some Philip-K-Dick level brain re-wiring not to reach for

      "%d things I hate about f-stringsn" % (int(many())) 
    

    modes of thinking.

  • Post Author
    alterom
    Posted February 3, 2025 at 4:23 am

    Yeah, I really missed ubiquitous C preprocessor macros in C++, so let's bring them back, but now inside string literals. Sweet.

    Seriously, I just keep being amazed that people are running with the idea of having a full-blown untyped and unchecked formatting mini language (that's what libfmt, which became C++20 format, literally calls it) inside string literals — i.e., the part of the source code that you're specifically telling the compiler to not treat as code.

    Think about it for a minute.

Leave a comment

In the Shadows of Innovation”

© 2025 HackTech.info. All Rights Reserved.

Sign Up to Our Newsletter

Be the first to know the latest updates

Whoops, you're not connected to Mailchimp. You need to enter a valid Mailchimp API key.