from Hacker News

Modern C++ – RAII

by green7ea on 5/30/25, 9:20 AM with 106 comments

  • by kjuulh on 5/30/25, 10:49 AM

    I am by no means a C++ expert, a noob rather. It might be possible to make this generic, but it seems quite easy to forget a small detail here, and then be kneecapped because you forgot one of the overloads. So that in very few amount of edge cases your abstraction isn't actually cleaned up.

    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

    My pet peeve with C++ RAII is that it uses destructors for cleanup, so it can’t throw an exception from within the destructor if that fails somehow. E.g. from the manual page of close(2):

    > 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

    About a year ago I bought the official C++ book and read through it fully (including most of the reference part) after finding out that Animal Well was written in it. It honestly seems like a very good and interesting langauge, if carefully used, especially for games where memory can be allocated at startup and held forever. Kinda wish I had an excuse to write some C++.
  • by yahoozoo on 5/30/25, 11:01 AM

    The color/holding all over this article is annoying.
  • by 90s_dev on 5/30/25, 10:51 AM

    Why don't we just use shared_ptr most of the time? Is it really that inefficient?
  • by pjmlp on 5/30/25, 11:05 AM

    It is a diservice to C++, to paint RAII as modern C++.

    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

    > Embracing Resource Acquisition Is Initialization (RAII) makes C++ the safest, most productive and fun C++ has ever been.

    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.