6 ms·
The tutorial starts with cout << "Hello, World!" << '\n'; But the most "modern" way to write to the console is: std::println("Hello World!");
by jenadine 3y ago
The tutorial starts with
cout << "Hello, World!" << '\n';
But the most "modern" way to write to the console is:
std::println("Hello World!");
- nalgeon 3y agoMaybe one day someone will rate all "Modern C++" tutorials according to their level of modernity :)
- pokler 3y agoThis is from C++23, right? I wonder how available this is within compilers and how many shops plan on moving to this version of the standard and how long that will take. I imagine targeting C++17 or 20 for 'modern' would be practical enough considering thats what you are most likely to run into in a professional setting.
- steveklabnik 3y ago> This is from C++23, right? std::println is, yes. > I wonder how available this is within compilers https://en.cppreference.com/w/cpp/compiler_support https://en.cppreference.com/w/cpp/compiler_support says clang, gcc, and msvc all support it, though I don't know how recent those versions are off the top of my head. In my understanding, with this specific feature, if you want a polyfill for older compilers, or to use some more cutting-edge features that haven't been standardized yet, https://github.com/fmtlib/fmt https://github.com/fmtlib/fmt is available to you.
- wiseowise 3y ago> says clang, gcc, and msvc all support it, though I don't know how recent those versions are off the top of my head Where did you find it? Search by println yields no results.
- steveklabnik 3y agoFormatted output library <print>
- wiseowise 3y agoThanks!
- scatters 3y ago> I don't know how recent those versions are gcc 14 has not yet been released; going by past years, 14.1 should come out around the start of May (2024). clang 17 was released September (2023); note that you need to use libc++ (stlib=libc++), not libstdc++. VS 2022 17.7 has been out since August.
- steveklabnik 3y agoThank you!
- materielle 3y agoHonestly C++ is in a real pickle. So iostreams were a mistake, they shouldn’t have happened in the first place. And given that they happened, std::format should have been added 20 years ago, or at least in C++11. The problem is that many professional shops never used iostreams, and instead hand rolled their own format library. Now, they don’t really care about std::format, and probably will never use it, because it would involve migrating off of their hand rolled solution which is working great. On the other hand, for small shops and hobbyists that don’t have the resources to reimplement std, this is a huge quality of life improvement. Meanwhile, more modern languages like Rust are encroaching. And this is all just about how to print “Hello World!”. This situation is really emblematic of the challenges faced by the C++ committee. It is really impossible to please everyone, or even a majority, since the community is so fractured.
- vitaut 3y agoThis is not about printing hello world but about all formatted I/O which is a significant chunk of the library. To be fair formatted I/O is pretty broken in C as well but for completely different reasons.
- gdiamos 3y agoWhen did that happen? I used to write a lot of C++, but it's been years since I've had to use it everyday.
- jenadine 3y agoThis year (C++23)
- pauljurczak 3y agoIt has not happened yet...
- tejohnso 3y agoI immediately thought your critique would be to use std::endl instead of '\n' I was reading a lot of c++ core guidelines and modern c++ blogs and books while writing a little game a couple of years ago and std::println was definitely not the common suggestion for modern c++. I see it's probably because it wasn't even available at the time. Now I feel like I'd have to review everything that I thought was modern two years ago to confirm that it is still the modern way.
- jb1991 3y agostd::endl is actually not advised most of the time. Using \n is the better alternative unless you need a buffer flush.
- dundarious 3y agoIf stdout is going to a terminal (i.e., it hasn't been redirected to a file), then the standard library will by default flush on every '\n' anyway. Similarly for C with fputs, etc. In C you control this with setvbuf, and of course, in C++ with iostreams it's a huge mess of rdbuf spaghetti and probably involving std::ios_base::sync_with_stdio as well.
- fooker 3y agoThis is wrong, stdout is not flushed by default on every newline. You can observe this by printing out tens of thousands of lines with vs without explicit flushing, there will be a big performance difference.
- dundarious 3y agoEdit: My C preference and iostream hatred is showing again, because I answered a question that was mostly about C++ iostreams with results based around C stdio (I think iostream is possibly the worst standard IO API in existence for a popular language, so when using C++, I recommend using something custom calling out to OS syscalls or C stdio). I could easily be wrong about C++ iostreams, so the most relevant half of my original post is highly suspect. I'll test it out later, but my C stdio answer is as-is below. I've tested that I'm correct on glibc, where the behavior is documented here: https://www.gnu.org/software/libc/manual/html_node/Buffering-Concepts.html https://www.gnu.org/software/libc/manual/html_node/Buffering... I can't remember using an alternative platform that behaves differently. A better test than the one you ran is to look at the resulting syscalls from a loop of `fputs("a line of output.\n", stdout);`, vs. `fputs("a bit of output. ", stdout);`. Buffering accumulates strings in memory before an eventual write syscall, so looking at syscalls is an easy way to see the difference: a write per-'\n' means line buffering. Compare the syscalls of both using strace. I did so, and when stdout is to the terminal, I see lots of calls to `write(1, "a line of output\n", 17)` compared to `write(1, "a bit of output. a bit of output"..., 1024)`. (To confirm, manually insert an `fflush(stdout);` in the "a bit of output. " version, and you'll see lots of `write(1, "a bit of output. ", 17)`) Then compare both when redirecting stdout to a file, and you'll see both cases make large write-s of 4KB. Only adding explicit fflush-es will make either version go back to small write-s of 17B. Compiled the trivial test programs with `cc -O2 ./main.c -o ./main`
- beached_whale 3y agoThat doesnt work on a majority of systems yet. Heck, gcc 14 gets it in the 14.1 release next year.
- pauljurczak 3y agoTrue. This is not a modern C++. It is a futuristic C++. Use {fmt} library in the meantime.
- beached_whale 3y agoI feel modern c++ is not so much mew features, save a few like move, but embracing guarantees with things like raii types.
- bdw5204 3y agoWhat is the rationale behind changing how print statements work in C++? Is there something broken about using cout or is this just another case of C++ adding extra ways to do the same thing for the sake of adding extra ways to do the same thing? The only significant difference I see is that std::println automatically includes the newline which may or may not be desired behavior.
- maccard 3y agoiostreams were a mistake, is the shortest way to sum it up.
- f1shy 3y agoAbsolutely! Glad to see it ending
- vitaut 3y agoDid you mean endling?
- pauljurczak 3y agoNo, he meant backslashend.
- pjmlp 3y agoNot for everyone, iostreams are great.
- steveklabnik 3y agoWhat's fun is, because everything is decided in papers, we can find out why! https://github.com/cplusplus/papers/issues/884 https://github.com/cplusplus/papers/issues/884 Accepted paper here: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2093r14.html https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p20... > The proposed std::print function improves usability, avoids allocating a temporary std::string object and calling operator<< which performs formatted I/O on text that is already formatted. The number of function calls is reduced to one which, together with std::vformat-like type erasure, results in much smaller binary code (see § 13 Binary code). Additionally, > Another problem is formatting of Unicode text: > std::cout << "Привет, κόσμος!"; > If the source and execution encoding is UTF-8 this will produce the expected output on most GNU/Linux and macOS systems. Unfortunately on Windows it is almost guaranteed to produce mojibake despite the fact that the system is fully capable of printing Unicode
- einpoklum 3y agoThat's the modern-modern C++ :-) More seriously, though - the language is changing gradually: Features are introduced (and rarely, deprecated); and the missing bits of the standard library are added. C++11 was a _very_ significant change - but not to how you print output. C++20 and C++23 introduced `std::format` and `std::print`.
- angiosperm 3y agoC++20 is as big a change as C++11. It is almost as much more pleasant upgrading to C++20 as was upgrading to C++11. As more of the C++20 features get implemented in the compiler you use, the experience will continue to improve. Many people can already write "import <std>;" instead of many lines of "#include <...>".
- NekkoDroid 3y agoits `import std;`, without the angled brackets. Just make sure you are using the namespaced fixed integer types if you do :) else you still need `import std.compat` which is basically the c-headers
- tobinfricke 3y agoVery Pascal-esque
- moffkalast 3y agoGod forbid it actually figures out what the type is and prints it out properly like every other language in existence? If so I'm sold.
- bingo3131 3y agoYes. The main feature this is built upon is C++20's new string formatting features (very heavily inspired by the third party libfmt). C++23 just adds convinience functions to format and print at the same time. Type detection, formatting options, positional-arguments, custom formatters for custom types and probably more are all supported.
- UncleOxidant 3y agoThis comment right here is modern C++ in a nutshell. The most "modern" way will likely be different next year.
- angiosperm 3y agoBut either way is fine.
- fsloth 3y agoYou don’t need to keep up with the joneses to write C++. The whole point with the language is that it’s for millions of lines of code that exist for decades and you don’t update every line every time the standard is updated. Most existing code probably uses output streams with << operators, it’s good to know what that does.
- f1shy 3y agoI‘ve hated cout from day 0. happy to learn this. Thanks!
- inetknght 3y ago> But the most "modern" way to write to the console is: Indeed, as mentioned [0] at cppcon 2022 :) [0]: https://youtu.be/eD-ceG-oByA?list=PLHTh1InhhwT6c2JNtUiJkaH8YRqzhU7Ag&t=4980 https://youtu.be/eD-ceG-oByA?list=PLHTh1InhhwT6c2JNtUiJkaH8Y...
- 3836293648 3y agoSure, but that still doen't have compiler support in compilers people actually have
- pjmlp 3y agoNot for portable code, C++23 is still quite far from being widely available.