Boost logo

Boost :

From: Eric Weitzman (eweitzman_at_[hidden])
Date: 2000-10-19 14:24:11


Steve,

I don't follow your explanation for the last line:

> static void test2()
> {
> std::vector<BaseNodePtr> v0;
> std::vector<DerivedNodePtr> v1;
>
> v1.push_back(DerivedNodePtr(new DerivedNode));
> v0.push_back(v1[0]);
> }
>
> The last line of code there constructs a new BaseNodePtr from a
> DerivedNodePtr, which compiles because there's an implicit pointer
> conversion from DerviedNode to BaseNode.

v1[0] is a DerivedNodePtr, not a DerivedNode, so the implicit pointer
conversion from DerivedNode to BaseNode isn't applicable. Let's say
BOOST_SMART_PTR_CONVERSION was defined, creating the conversion operator
T*(). Now, wouldn't the compiler then have to apply two functions to get
v1[0] from a DerivedNodePtr to a BaseNodePtr -- conversion T*() followed by
constructor shared_ptr(T*)? The one-level-of-implicit-conversion rule
doesn't prevent this sequence, but I don't understand what enables it.

Wouldn't this work instead?

     v0.push_back(v1[0].get());

> I'm going to assume now that you're using MSVC, which won't compile the
above.

Yes, I'm using MSVC. Why won't it compile this? Is the problem the template
constructor?

   template<typename Y>
      shared_ptr(const shared_ptr<Y>& r) : px(r.px) { // never throws
         ++*(pn = r.pn);
      }

Thanks,
- Eric


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