Wednesday, 27 January 2016

Another deleted answer of mine from Stack Overflow:
As I can see, smart pointers are used extensively in many real-world C++ projects.
True but, objectively, the vast majority of code is now written in modern languages with tracing garbage collectors.
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.
That's a bad idea because you still need to worry about cycles.
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?)
Oh wow, there are so many things wrong with your line of thinking:
  1. Boehm's GC is not a "proper" GC in any sense of the word. It is truly awful. It is conservative so it leaks and is inefficient by design. See:
  2. Shared pointers are, objectively, nowhere near as popular as GC because the vast majority of developers are using GC'd languages now and have no need of shared pointers. Just look at Java and Javascript in the job market compared to C++.
  3. You appear to be restricting consideration to C++ because, I assume, you think that GC is a tangential issue. It isn't (the only way to get a decent GC is to design the language and VM for it from the beginning) so you are introducing selection bias. People who really want proper garbage collection don't stick with C++.
What are the reasons for using reference-counting smart pointers?
You are restricted to C++ but wish you had automatic memory management.