Boost logo

Boost :

From: Kevlin Henney (Kevlin.Henney_at_[hidden])
Date: 2000-02-08 11:09:31


Miki Jovanovic wrote:
>Here is my proposition for dynamic_cast for shared_ptr. Would this be
>of value?
[snip]

This seems quite involved compared with the one posted on this list a few
weeks ago:

      template<typename Target, typename Source>
      inline shared_ptr<Target> dynamic_shared_cast(const
shared_ptr<Source> & arg)
      {
          shared_ptr<Target> result;

          if(Target * got = dynamic_cast<Target *>(arg.px))
          {
              result.px = got;
              delete result.pn;
              ++*(result.pn = arg.pn);
          }

          return result;
      }

The only inefficiency here is that an unused count is allocated and then
discarded because shared_ptr currently counts nulls. My own counted pointer
implementations do not and the control flow was lifted pretty much
straight.

Kevlin


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk