1

I'm going to use downcasting in my project in order to cast one boost::shared_ptr<> to another one in class hierarchy. That is my test code where I am using boost/polymorphic_pointer_cast and boost::dynamic_pointer_cast both variant work. But I don't understand what is difference between them. Could you describe me what is difference between boost::polymorphic_pointer_downcast and boost::dynamic_pointer_cast?

namespace cast
{

class Base
{
public:
    virtual void kind() { std::cout << "base" << std::endl; }
};

class Derived : public Base
{
public:
    virtual void kind2() { std::cout << "derived" << std::endl; }
};

}

int main(int argc, char* argv[])
{
    try
    {
        boost::shared_ptr<cast::Base> base(new cast::Derived());
        base->kind();

        boost::shared_ptr<cast::Derived> d1 = boost::polymorphic_pointer_downcast<cast::Derived>(base);
        d1->kind();
        d1->kind2();

        boost::shared_ptr<cast::Derived> d2 = boost::dynamic_pointer_cast<cast::Derived>(base);
        d2->kind();
        d2->kind2();
    }
    catch (const std::exception& e)
    {
        std::cerr << "Error occurred: " << e.what() << std::endl;
    }

    return 0;
}

Thanks.

Space Rabbit
  • 141
  • 2
  • 11
  • 1
    full documentation and reasoning here: http://www.boost.org/doc/libs/1_59_0/libs/conversion/cast.htm However, whatever you're trying to do, it smells really bad from here. Think of another way. – Richard Hodges Apr 18 '17 at 22:04
  • @RichardHodges I read this documentation but don't understand what is difference between boost::polymorphic_pointer_downcast and boost::dynamic_pointer_cast... – Space Rabbit Apr 19 '17 at 08:30
  • @SpaceRabbit looking at the names alone, I don't expect these casts to do the same thing. That's key. – sehe Apr 19 '17 at 09:13
  • @sehe So, what is difference? – Space Rabbit Apr 19 '17 at 09:16
  • Well. Without actually reading the documentations, I'd expect the former to do downcasting using only static type information (so the compiler can optimize to a constant vtable offset), and the latter does a dynamic_cast on a pointer, see e.g [here](http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast) – sehe Apr 19 '17 at 09:20
  • @sehe What is better to use? first one or second one? – Space Rabbit Apr 19 '17 at 09:24
  • Mmm. I somehow was confused, and thinking of static casts. Sorry. Will read a bit more on the polymorphic casts. – sehe Apr 19 '17 at 09:27

1 Answers1

3

According to the docs the polymorphic down-cast is as efficient as a static downcast, BUT is does add the additional runtime type check in debug mode:

The C++ built-in static_cast can be used for efficiently downcasting pointers to polymorphic objects, but provides no error detection for the case where the pointer being cast actually points to the wrong derived class. The polymorphic_downcast template retains the efficiency of static_cast for non-debug compilations, but for debug compilations adds safety via an assert() that a dynamic_cast succeeds

A logical consequence is

A polymorphic_downcast should be used for downcasts that you are certain should succeed

Otherwise, use boost::dynamic_pointer_cast.


(Note the *_pointer_*cast versions merely add genericity for smart pointer types, but the docs above apply unchanged to these versions.)

Mihai Todor
  • 8,014
  • 9
  • 49
  • 86
sehe
  • 374,641
  • 47
  • 450
  • 633