by green7ea on 5/30/25, 9:20 AM with 106 comments
by kjuulh on 5/30/25, 10:49 AM
Better than nothing, and might be the most preferred way of doing things in C++, but it does seem dangerous to me. ;)
by nathell on 5/30/25, 11:15 AM
> A careful programmer will check the return value of close(), since it is quite possible that errors on a previous write(2) operation are reported only on the final close() that releases the open file description. Failing to check the return value when closing a file may lead to silent loss of data. This can especially be observed with NFS and with disk quota.
Last time I checked, GCC’s implementation of ~ofstream() ignored failures from the underlying close().
by 90s_dev on 5/30/25, 11:27 AM
by yahoozoo on 5/30/25, 11:01 AM
by 90s_dev on 5/30/25, 10:51 AM
by pjmlp on 5/30/25, 11:05 AM
We were already making use of RAII in C++ compilers for MS-DOS during the C++ARM (the C++ equivalent of K&R C book), one such example would be Turbo Vision C++ variant and Borland International Datastructures Library (BIDS).
If anything, it is a pity that 30 years later, it is still something we need to educate people about, as it isn't as adopted as it should be.
EDIT:
If this is supposed to be Modern C++, at the very least provide C++23 example in 2025, instead C++ from 2011.
Meaning import std, using a custom deleter in std::unique_ptr (it doesn't manage only heap types), making use of =default to get back compiler generated member functions, as it would do the right thing in the example for the fd handle.
by growlNark on 5/30/25, 10:23 AM
This seems like an extremely low bar.
Anyway, what use is there for C++ in 2025 aside from maintenance of legacy codebases and game engines? Off-hand I'd say C++ programmers are twice as expensive as rust programmers for the same semantics, and then you're still stuck with a C++ programmer you need to figure out how to socialize with.