r/cpp • • Oct 18 '23

What does C++ do better than other languages?

Purely from a language design perspective, what does C++ do better than other languages?

296 Upvotes

443 comments sorted by

View all comments

21

u/Tari0s Oct 18 '23

for me one of the most amazing features of cpp is deterministic livetime of objects. To know for each objekt when it gets constructed and when it gets destructed is such a benefit over other languages.

1

u/nelusbelus Oct 18 '23

Singletons joined the gang

2

u/CocktailPerson Oct 18 '23

The typical Meyers' singleton will be constructed on first use and destroyed when the program exits.

-2

u/and69 Oct 18 '23

Do you really need to know 100% when objects are constructed and destructed? With shared_ptr, this is not always true.

In C# for example, you dont care when objects are destructed, but you can enforce deterministic destroy time when needed.

18

u/HappyFruitTree Oct 18 '23

With shared_ptr, this is not always true.

You know it will be destroyed when the reference count drops to zero.

8

u/AntiProtonBoy Oct 18 '23

Do you really need to know 100% when objects are constructed and destructed?

Yes. Deterministic destruction allows you to reason about your code's behaviour and resource usage. Destructors are not just about memory either. They can also serve as mechanism for freeing resource locks or signal availability for a particular resource. Synchronisation primitives is one such use case. You'd want to know when these exactly happen.

With shared_ptr, this is not always true.

Well, depends how you look at it. shared_ptr was specifically designed with lifetime guarantees in mind. It guarantees that the resource is available for instances holding onto it, and guarantees pointer destruction when all references to it was released. Determinism is guaranteed from the perspective of the reference counter.

Your argument stems from the fact that you will never know what and when will hold onto a shared_ptr, but that's okay, because you are opting into this by design. In such cases you'd have to treat shared_ptr as having "global variable" semantics.

6

u/stinos Oct 18 '23

In C# for example, you dont care when objects are destructed, but you can enforce deterministic destroy time when needed.

You can implement IDisposable but as long as your caller doesn't implement the pattern, i.e. calls Dispose in some way, it's not really deterministic since it's up to the GC when it happens. Or did you mean something else?

Do you really need to know 100% when objects are constructed and destructed

Truly need, no, but it's for sure extremely convenient to be able to. Probably because I'm used to it, but in other languages it's one of the features in C++ I miss most.

3

u/[deleted] Oct 18 '23

IDisposable doesn't garbage collect. it exists for cleaning up (unmanaged) resources. The object that implements IDisposable is garbage collected like any other (i.e. potentially never)

3

u/stinos Oct 18 '23

I'm aware, sorry if it came over otherwise

2

u/Tari0s Oct 18 '23

yeah okay shared_ptr are not always deterministic, but i never needed to use them in the last few years. That doesn't mean you don't need them, but most of the time a shared_prt is also a code smell.

To answer your first question. deterministic object destruction has a few benefits, when your brain is accommodated to the concept. When opening a file or stream it will always be close on object destruction. The same is for locks, they get always unlocked after aquirering them.

when you deal with exceptions in you code or a lot of return statement it is always garanteed that the resources get freed, because you can't overlook a return path or forget to implement a finally case.

1

u/BobbyThrowaway6969 Oct 18 '23

Do you really need to know 100% when objects are constructed and destructed?

Yes. For various reasons.

1

u/BobbyThrowaway6969 Oct 18 '23

It lets you do cleanup when the scope ends. Like UE4's TGuardedValue for example. It's so simple and useful. Also makes profiling easy.

Also Goto/label. Love them or hate them, they may be dangerous if you don't know what you're doing but they do have valid usecases.