4 ms·
"For example this seems ok to me, because the type is on the left" auto* x = new Foo(); Are you only referring to the fact that the type is noted at least
by EpicEng 12y ago
"For example this seems ok to me, because the type is on the left"
auto* x = new Foo();
Are you only referring to the fact that the type is noted at least once on that line? I ask because that would not pass my code review at all 99% of the time (manually allocating memory that way.)
As for something like
auto x = std::move(y);
I couldn't say it is bad without seeing the context around it. Is it a small function? Is it obvious what y is? Looks fine to me in most cases.
- TheSoftwareGuy 12y agodo you NEVER allow dynamically allocated memory? or am I misunderstanding you?
- Nursie 12y agoIn modern c++ you can get away without it by using a combination of RAII and the various shared pointer and container features. It's an odd mental leap for this old C hack, but it works quite well when you get into it. Bonus - no 'free' or 'delete' necessary.
- TheSoftwareGuy 12y agoPlease correct me if I am wrong, but RAII isn't flexible enough to allow for lazy initialization. the various STL pointer types (std::shared_ptr etc..) are great, but they do incur overhead, and sometimes that is not acceptable in e.g. embedded systems.
- cottonseed 12y agoI think he means it should read something like: auto x = std::make_unique<Foo>(); Unless you're implementing an allocation policy, you shouldn't be calling new and delete directly.
- EpicEng 12y agoThere are very few instances in which manually managing memory in C++ is justified. When you have `unique_ptr`, `shared_ptr`, and all sorts of container classes, it simply doesn't come up in most cases. If you see operator new being used you should be suspicious.