News for hackers. Stuff that matters.
Re:C++23: The Next C++ Standard by nuancebydefault on Tuesday July 11, 2023 @12:13PM (#36683100)
Are there now in c++, after all these years, f-strings like python has, or at least something coming close? If not, I keep being at my disappointed state about c++.
Re:C++23: The Next C++ Standard by Longhanks on Tuesday July 11, 2023 @12:27PM (#36683319)
Yes, already since C++20. But if you really would have wanted to change your mind, you could’ve found out easily.
Re:C++23: The Next C++ Standard by LordDragonfang on Tuesday July 11, 2023 @12:43PM (#36683554)
>Already since C++20

...Or phrased another way, they finally added them in the latest spec (which the majority of C++ devs aren't even using yet)

Re:C++23: The Next C++ Standard by akhosravian on Tuesday July 11, 2023 @03:51PM (#36686396)
C++23 was finalized nearly six months ago so it is not correct to call C++20 the “latest spec”.
Re:C++23: The Next C++ Standard by LordDragonfang on Tuesday July 11, 2023 @04:31PM (#36686837)
I should have said the "latest standard", not "spec", if we're being technical. But EVERY bit of official material is very clear about asserting that C++23 is still a preview/in-progress, not a standard. Saying otherwise is, strictly speaking, incorrect.

https://isocpp.org/std/the-standard

https://www.iso.org/standard/79358.html

https://github.com/cplusplus/draft/blob/main/papers/n4951.md

And quite frankly, what matters to devs is what tooling supports the specification without special configuration, and the answer is "basically none". Not a single compiler fully supports it.

https://en.cppreference.com/w/cpp/compiler_support/23

Also, side note, I'd be surprised if more than a third of C++ devs were even using C++20, based on last year's numbers showing only ~25% adoption:

https://www.jetbrains.com/lp/devecosystem-2022/cpp/

Re:C++23: The Next C++ Standard by SubjectToChange on Wednesday July 12, 2023 @03:35AM (#36691456)
fmt has been available for years and it works with ridiculously old compilers. It’s great to have it standardized but it’s not a new capability that C++ didn’t already have.
Re:C++23: The Next C++ Standard by psychphysic on Tuesday July 11, 2023 @12:47PM (#36683645)
Most people don't have time to spend repeatedly checking for features in languages they don't use.
Re:C++23: The Next C++ Standard by Longhanks on Tuesday July 11, 2023 @01:04PM (#36683907)
If people don’t have time to keep up with a languages updates (which, in case of C++, is currently _once_ every three years), then they don’t have time to complain about the lack of features, either. This one had the time to complain and just didn’t want to bother typing "c++ string formatting", which would have been fewer keystrokes than the comment complaining.

On DuckDuckGo, the very first result for "c++ string formatting" is the exact thing this person was complaining about.

Re:C++23: The Next C++ Standard by nuxi on Tuesday July 11, 2023 @01:34PM (#36684398)
OP asked about “f-strings like python has”, which is different from plain std::format().
Re:C++23: The Next C++ Standard by psychphysic on Tuesday July 11, 2023 @01:32PM (#36684363)
But then he'd have missed out on your conversational effervescence.
Re:C++23: The Next C++ Standard by JonChesterfield on Tuesday July 11, 2023 @08:23PM (#36688931)
Google finds https://stackoverflow.com/questions/62781161/is-it-possible-... which is a portal to horrors with the answer "no"
Re:C++23: The Next C++ Standard by nuancebydefault on Tuesday July 11, 2023 @12:50PM (#36683678)
That about wanting to change mind... it touches a string! Somehow the let-downs must have been too much for me at a certain point in time. That being said, I'm curious to find out right now. Edit... no such thing found as string interpolation in cpp, at least not in my first 4 search hits. I'll crawl back.
Re:C++23: The Next C++ Standard by Conscat on Tuesday July 11, 2023 @01:03PM (#36683891)
String interpolation is currently blocked by:

- template-function parameters (NTTPs with a function parameter syntax rather than a template parameter syntax, tentatively spelled as `void foo(template int constant){}` )

- Scalable reflection, in combination with expansion statements (likely in C++26, spelled `template for (auto& e : array) {}` ) which would allow you to write an arbitrary parser for the template string into real C++ code.

Syntax2 already supports string interpolation as a special compiler feature.

Re:C++23: The Next C++ Standard by coldtea on Tuesday July 11, 2023 @01:12PM (#36683999)
std::string formatted_str = std::format( "My name is {} and my favorite number is {}", name, num);
Re:C++23: The Next C++ Standard by 0xffff2 on Tuesday July 11, 2023 @01:17PM (#36684085)
That's really nothing like python strings, which for the same string would be

    formatted_str = f"My name is {name} and my favorite number is {num}"
The function call I could forgive, but the the ability to embed arbitrary python expressions into the f-string directly is a huge readability win IMO.
Re:C++23: The Next C++ Standard by interroboink on Tuesday July 11, 2023 @01:41PM (#36684488)
Meanwhile Perl's over here like "we had this in the 90s!" (:
Re:C++23: The Next C++ Standard by gpderetta on Tuesday July 11, 2023 @02:42PM (#36685435)
Of course posix shells gad that even before. How far we can go? What's the first language that had something that resembles string interpolation?
Re:C++23: The Next C++ Standard by interroboink on Tuesday July 11, 2023 @02:55PM (#36685643)
I'm sure someone here can say something about Lisp machines (:
Re:C++23: The Next C++ Standard by coldtea on Tuesday July 11, 2023 @07:51PM (#36688708)
Well, if that's the syntax, I'd rather not have it!
Re:C++23: The Next C++ Standard by coldtea on Tuesday July 11, 2023 @07:50PM (#36688704)
That type of interpolation is something most non-scripting languages don't have anyway, and it took Python several decades to get it, and only has had it in the 5 years or so.
Re:C++23: The Next C++ Standard by paulddraper on Wednesday July 12, 2023 @12:43PM (#36697410)
You misunderstand.

C++20 added std::format [1], similar to Python's str.format [2] (~2010).

But OP said f-strings [3] (2016) which is string interpolation, a language-level feature.

C++ does not have string interpolation.

Not massively important, but IMO quite handy for inline code, e.g. SQL parameterization.

[1] https://en.cppreference.com/w/cpp/utility/format/format

[2] https://docs.python.org/3/library/stdtypes.html#str.format

[3] https://docs.python.org/3/reference/lexical_analysis.html#f-...

Re:C++23: The Next C++ Standard by enriquto on Tuesday July 11, 2023 @12:18PM (#36683170)
You still have cstdio, which is perfectly cromulent.

There's also a format-like thing using integers surrounded by curly braces, but it's horrific and I'm afraid to copy-paste it here.

Re:C++23: The Next C++ Standard by Conscat on Tuesday July 11, 2023 @01:05PM (#36683922)
cstdio is not extensible, which in practice makes it useless for most C++ code.
Re:C++23: The Next C++ Standard by gpderetta on Tuesday July 11, 2023 @02:42PM (#36685448)
And not type safe which make it even worse.
Re:C++23: The Next C++ Standard by LordDragonfang on Tuesday July 11, 2023 @12:47PM (#36683637)
Slightly off topic, but I recently learned that implementing the opposite of what you've asked for, bitshift stdout in python, is only a few lines of code:

    class cppstream:
        def __lshift__(self, right):
            print(right, end='')
            return self
    cout = cppstream()
    endl = "\n"
    
    cout << "This" << " is" << " cursed" << "!" << endl
Re:C++23: The Next C++ Standard by ziml77 on Tuesday July 11, 2023 @01:02PM (#36683873)
I hate that... but actually less so than I hate Pandas' abuse of operator overloading on the indexing operator.