Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::shared_polymorphic_downcast is gone in 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 the commit (r81463) contains only the cryptic log message "Update shared_ptr casts."

It is not clear to me what I should be using now instead, or why this functionality was removed. Can anybody help?

EDIT: Thanks everyone for the insightful comments. I find myself a bit frustrated that boost will make backwards-incompatible changes in a release without any justification or notice, and I also find it frustrating that they remove useful features. But based on the responses, I can do what I want in two lines of code instead of one, so I think that will suffice. Still, I am leaving this question "unanswered" because nobody has provided a simple way to get the old behavior of boost::shared_polymorphic_downcast; that is, to use a dynamic_cast when debugging is enabled and a static_cast when it is not.

like image 475
Jim Garrison Avatar asked Feb 07 '13 03:02

Jim Garrison


1 Answers

Use boost::dynamic_pointer_cast.

The update it talks about is to match the design of C++11. In C++11 the pointer casts are generalized as the functions std::dynamic_pointer_cast (and friends) to allow us to write:

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

So that PointerToBase could be a raw Base* or a std::shared_ptr<Base>, without us writing cases.

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

like image 65
GManNickG Avatar answered Sep 21 '22 11:09

GManNickG