I want to know whether or not the standard committee has fixed the infamous Hello, world! bug. I'm primarily talking about the new <print>
library (not yet available in any of the compilers).
The {fmt} library (which has inspired the standard library) has not fixed this. Apparently, it does not throw any exceptions when outputting to /dev/full
(as of v9.1.0). So the use of C I/O functions like std::fflush
for error handling is still a thing.
The below program notices the error and returns a failure code (thus not buggy):
#include <exception>
#include <cstdio>
#include <cstdlib>
#include <fmt/core.h>
int main()
{
fmt::println( stdout, "Hello, world!" );
if ( std::fflush( stdout ) != 0 || std::ferror( stdout ) != 0 ) [[unlikely]]
{
return EXIT_FAILURE;
}
}
But is this possible in C++23?
#include <print>
#include <exception>
#include <cstdio>
#include <cstdlib>
int main()
{
try
{
std::println( stdout, "Hello, world!" );
}
catch ( const std::exception& ex )
{
return EXIT_FAILURE;
}
}
For those unaware of the "Hello World" bug, the below program (in Rust) panics and outputs a useful error message:
fn main()
{
println!( "Hello, world!" );
}
./main > /dev/full
thread 'main' panicked at 'failed printing to stdout: No space left on device (os error 28)', library/std/src/io/stdio.rs:1008:9
Conversely, C++ standard iostreams
along with some other languages (C, Ruby, Java, Node.js, Haskell, etc) don't report any failure by default even at program shutdown when the program closes the file streams. On the other hand, some others (Python3, Bash, Rust, C#, etc) do report the error.