3 ms·
it more like 99% of the time and as long you don't go near new or malloc you generally can't leak. Then with RAII you can clean up any kind of resource. Most l
by sqeaky 10y ago
it more like 99% of the time and as long you don't go near new or malloc you generally can't leak.
Then with RAII you can clean up any kind of resource. Most languages have pretty rudimentary tools for closing files and worse for releasing mutexes. These tasks are almost easy to get right in modern C++.
- WildUtah 10y agoI still love 'with-open-file and its sisters in LISP. Get that resource in one line and it's guaranteed to be cleaned up right without ever thinking about it again. Then you have total freedom to use it however you want and the results destructure themselves.
- civility 10y agoYou could implement something comparable: int main() { with_open_file_for_writing("foo.dat", [](FILE* fp) { fprintf(fp, "old style I/O, automatically closed\n"); }); return 0; }
- Animats 10y ago"With" is even better in Python. Exceptions during closing work right. The big problem with RAII is that if anything goes wrong in a destructor, you're in trouble.