2 ms·
The C++ way is unique_ptr, which covers about 80% of the needs and in cases where it works it is probably better than a GC. Shared_ptr covers about 15% of the
by bluGill 19d ago
The C++ way is unique_ptr, which covers about 80% of the needs and in cases where it works it is probably better than a GC. Shared_ptr covers about 15% of the cases, but that still leaves 5% where you need a full better GC.
GC is another runtime dependency (or a really complex intrusive addition to your code), so if you don't absolutely need it I would avoid it in C++. A lot of C++ programs can avoid it with careful design.
If you need a better GC anyway, in the real world modern GC algorithms are likely more performant for your use case than shared_ptr. Thus shared_ptr should be thought of as the C++ answer only because a "real" GC is hard to implement, but if you implement a real GC anywhere just use it to replace shared_ptr as well. (I lean to use unique_ptr even when GC is available, but I reserve the right to change my mind if someone presents evidence)