As I can see, smart pointers are used extensively in many real-world C++ projects.
Though some kind of smart pointers are obviously beneficial to support RAII and ownership transfers, there is also a trend of using shared pointers by default, as a way of "garbage collection", so that the programmer do not have to think about allocation that much.
Why are shared pointers more popular than integrating a proper garbage collector like Boehm GC? (Or do you agree at all, that they are more popular than actual GCs?)
I know about two advantages of conventional GCs over reference-counting:
- Conventional GC algorithms has no problem with reference-cycles.
- Reference-count is generally slower than a proper GC.
What are the reasons for using reference-counting smart pointers?
std::unique_ptr
is sufficient and as such has zero-overhead over raw pointers in terms of run-time performance. By usingstd::shared_ptr
everywhere you'd also obscure the ownership semantics, losing one of the major benefits of smart pointers other than automatic resource management -- clear understanding of the intent behind the code.