Friday, January 31, 2014

FeedaMail: Comments for Sutter’s Mill

feedamail.com Comments for Sutter's Mill

Comment on GotW #89 Solution: Smart Pointers by J. J. Lee

Is there any helper template to relieve pain from make_unique to make_shared, if I decide to change from unique_ptr to shared_ptr later?

For example,

  template <class SP> class smart_ptr_switch  {      template <class... Args> static SP make_smart(Args&&... args)      {          if (SP is some kind of unique_ptr<T>) {              return std::make_unique<T>(new T(std::forward<Args>(args)...));          } else if (SP is some kind of shared_ptr<T>) {               return std::make_shared<T>(new T(std::forward<Args>(args)...));          } else {               static_assert ???          }      }  };  

So I can typedef my_smart_pointer to a std::unique_ptr first. If later I change typedef my_smart_pointer to a std::shared_ptr, I don’t need to change all std::make_unque to std::make_shared.

If I call smart_ptr_switch::make_smart(…) and typedef std::unique_ptr my_smart_pointer, this method will return a result from make_unique.
If I call smart_ptr_switch::make_smart(…) and typedef std::shared_ptr my_smart_pointer, this method will return a result from make_shared.

Read More »

Comment on GotW #93 Solution: Auto Variables, Part 2 by Auto Pilot | LightSleeper

[…] For even more recommendations on why auto is a good idea (efficiency, guaranteed initialization, and correctness), see Herb’s GotW #93. […]

Read More »
 
Delievered to you by Feedamail.
Unsubscribe

No comments:

Post a Comment