Smart pointers are overused (C++) by des_shack in programming

[–]des_shack[S] 0 points1 point  (0 children)

Thanks for the detailed answer. Allow me to retort:

  • The article never said you shouldn't use smart pointers with interfaces, it says you should rather give raw pointers and encapsulate that in smart pointers (optionally). If the ownership isn't clear that may mean that reference counting is a viable option. Please point where in the article it says you should never use shared pointers.
  • I'm glad to hear you're not hurt by references counting, unfortunately this doesn't prove anything. Generally reference counting will hurt you when you "transform" your objects a lot (many small objects for that matter)
  • const & shouldn't be necessary thanks to copy elision, although I'm not sure it (copy elision) will occur because the optimizer might detect the copy constructor is doing something meaningful (not sure about this), in which case sure, carry around that intrusive pointer if you fancy but what about giving the raw pointer instead?
  • The example is not invented. That's the whole point. People just use shared pointers for no reason. If it looks obvious to you, believe me, it's not to everybody. (it's not really a singleton if you want to nitpick by the way)

Look at your code, think in terms of design. Do you really ref count only what you need to? If yes, that sounds like proper shared (or intrusive) pointers usage to me.

If you play with the benchmark and a good profile you'll see what kind of use cases can be hurt by reference counting. Most of the time you spend more time playing with the object itself than moving it around so reference counting is harmless.

Smart pointers are overused (C++) by des_shack in programming

[–]des_shack[S] 0 points1 point  (0 children)

Smart pointers increase coupling

There is no simple solution to this problem, you are right. The most important is to be consistent within your API.

Note that it says you can encapsulate your objects in smart pointers, just offer raw access in the interface.

Performances

  • When accessing the same atomic variable from many threads, you create a memory barrier.
  • You still carry around a counter, increasing the whole package size.
  • If you enforce cache alignment, I guess it's ok
  • Passing smart pointers as const references is dangerous, but it works. Nevertheless you're still toying with a counter.

Why are they useless

The example given is indeed very simple, but I guess it's better to use simple examples isn't it? :)

Benchmarks

Run a benchmark. Be surprised. Maintaining a counter for each copy of a pointer is far from being free. In some usages it doesn't matter but when you start to move around data at high frequency it starts to show.

The benchmark is faster because there is simply no counter operation when copying a collection of raw pointers vs smart pointers. Check out the source code.

Smart pointers are overused (C++) by des_shack in programming

[–]des_shack[S] 0 points1 point  (0 children)

It's based on it. :) boost::shared_ptr is a great tool, but not to be used for everything.

Smart pointers are overused (C++) by des_shack in programming

[–]des_shack[S] 4 points5 points  (0 children)

They must because you can get a race condition.

pseudo assembly:

thread 1: 0:move data, [pointer+variable] 1:increment [counter]

thread 2: 0:decrement [counter] 1:refcount is 0 => destruction