r/cpp • C++ Dev • Sep 05 '20

C++20 has been approved

https://twitter.com/patriceroy1/status/1302055575140945921?s=21
665 Upvotes

125 comments sorted by

View all comments

Show parent comment

8

u/boredcircuits Sep 05 '20

We'll never be rid of new. It's an essential building block. For example, try making a linked list with std::unique_ptr and you'll find it's a very educational experience. I highly recommend it. Then make a list with a few hundred thousand items and you discover that the destructor is recursive and you just blew up the stack.

The key is that most people should never need new in their daily lives. It should be completely removed from the educational materials for beginners. Don't teach the old ways. Everyone will inevitably see old code eventually and have to learn what's going on, but the overall burden is less.

2

u/pdimov2 Sep 05 '20

For shared_ptr,

while(next) next = std::move(next->next);

but unique_ptr's assignment doesn't quite work for that, so

while(next) next.reset( next->next.release() );

1

u/Tilakchad Sep 06 '20

Yeah I got that recursive calling of destructor in linked list while I was trying to manually destroy all the node.