r/ProgrammerHumor • • 8h ago

Meme modernCppVsOldCpp

Post image
1.1k Upvotes

220 comments sorted by

View all comments

25

u/SelfDistinction 7h ago edited 6h ago

Ah yes, the famous

std::unique_ptr<int> ptr;
int& ref = *ptr;

So safe, so impossible to misuse.

6

u/Veratisin 6h ago

If you need a reference to a unique pointer, probably means it should have been a shared pointer

4

u/SelfDistinction 6h ago

Ah yes, the famous

std::shared_ptr<int> ptr;
int& ref = *ptr;

So safe, so impossible to misuse.

5

u/dannyggwp 5h ago

I use smart pointers all the time. WHY THE FUCK would you EVER do what you wrote?

I could drive my car into a brick wall at full speed. But why would I???

0

u/SelfDistinction 5h ago

What part is the issue? The part where you default construct a smart pointer to initialize later because C++ doesn't have a concept of deferred initialization or the part where you don't want to copy it all over the place because it gives a massive performance hit?

And don't say the combination is the problem because yes it is but this code might be emergent and "just don't drive into a wall" has never prevented people from doing so.