Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
How to avoid one C++ foot gun (pboyd.io)
5 points by todsacerdoti on May 11, 2024 | hide | past | favorite | 3 comments


I think I would be using std::unique_ptr here instead of the manual memory management, which as you can see sometimes fails spectacularly. I've had codebases myself ~10 years ago where I didn't really know how to use them properly, and I eventually completely gave up on them. The code was just too unreliable. These days, writing C++ is just like writing Python to me. The language is rarely the problem as long as I keep on the happy path with RAII.

I did see your issue immediately, though, and I could remember having the same issue with my own code. I remember assigning Shaders into a shader manager. They were already dead.

These days I prefer to in-place construct most things, unless there's a particular reason for assignment.


This is not a footgun. This is about the most basic things from the C++ object model you should know.

C++ is not Java. The first thing to learn is where things get allocated and how copying works, which is quite different from "almost-everything-in-the-heap" languages.


No offense, but this is just terrible C++ and the author needs to go back to the basics and understand how objects, references, and pointers work. His proposed "solution" is also terrible.

All you need is this:

https://gcc.godbolt.org/z/cEo9sYGTa




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: