Boost :: shared_polymorphic_downcast went into boost 1.53.0. What should i use instead?

boost::shared_polymorphic_downcast disappeared between boost 1.52.0 and 1.53.0 . Nothing is mentioned about this in the release notes, and commit (r81463) contains only the cryptographic message of the journal "Update shared_ptr casts."

It is not clear to me what I should use instead, or why this function was removed. Can anyone help?

EDIT: Thanks to everyone for the insightful comments. I'm a little upset that the promotion will make backward-incompatible changes to the release without any excuses or notifications, and I also find it disappointing that they remove useful features. But based on the answers, I can do what I want in two lines of code instead of one, so I think that will be enough. However, I leave this question β€œunanswered” because no one provided an easy way to get the old behavior of boost::shared_polymorphic_downcast ; that is, use dynamic_cast when debugging is enabled, and static_cast if it is not.

+6
source share
1 answer

Use boost::dynamic_pointer_cast .

The update he is talking about is consistent with the design of C ++ 11. In C ++ 11, pointer casting is generalized as a function of std::dynamic_pointer_cast (and friends) so we can write:

 template <typename PointerToBase> // models Base* in some way void foo(PointerToBase ptr) { auto ptrToDerived = std::dynamic_pointer_cast<Derived>(ptr); } 

Thus, PointerToBase can be raw Base* or std::shared_ptr<Base> if we do not write cases.

Boost, of course, just wants to match C ++ as much as possible.

+10
source

All Articles