Re: [Boost-users] A question about shared_ptr(Zhuo Hao)

But consider the following code: #include<iostream> using namespace std; class CMutex{};//Hide all the implementations for convience template<class Y> void world(Y * p) { } void hello(int *x){ } int main(){ hello(0); world<CMutex>(0); return 1; } It compiles and runs without problems.So you see I can pass zero as a pointer to Y, and as a pointer to int. That's OK. I wonder how it is implemented that we cannot pass zero as a pointer to Y in the constructor of shared_ptr. Thanks Zhuo Hao On 14/07/06, Tommy Hinks <tommy.hinks@gmail.com> replied: You can't pass 0 because the Type passed must be an Investment*, not just a type that could be cast to one. This is why you have to explicitly cast it yourself.

Zhuo Hao wrote:
<snap>
I wonder how it is implemented that we cannot pass zero as a pointer to Y in the constructor of shared_ptr.
Consider: template <class U> class P { public: template <class V> P(V * v) : u(v) {} private: U *u; }; Now "P<CMutex> p1(0);" will not compile but "P<CMutex> p1(new CMutex);" will. -delfin

On 7/16/06, Delfin Rojas <drojas@moodlogic.com> wrote:
template <class U> class P { public: template <class V> P(V * v) : u(v) {}
private: U *u; };
Now "P<CMutex> p1(0);" will not compile but "P<CMutex> p1(new CMutex);" will.
I believe this is due to the fact that in C++ it is illegal for the compiler to do more than one implicit cast. In this case it would have to implicitly cast from <int>(0) to <V *>(0) and then implicitly cast again from <V *>(0) to <U *>(0). Jon

I think the answer would be: At the point of type deduction ctor does not know what type V is: It knows that U is CMutex, but V is unknown. The following sample would compile just fine: class A1 {}; class A2 : public A1 {}; A2* a_null_ptr=NULL; P<A1>(a_null_ptr); or P<A1>(static_cast<A2*>(NULL)); Best Regards, Ovanes Markarian _____ From: Jonathan Franklin [mailto:franklin.jonathan@gmail.com] Sent: Monday, July 17, 2006 23:52 To: boost-users@lists.boost.org Subject: Re: [Boost-users] A question about shared_ptr(Zhuo Hao) On 7/16/06, Delfin Rojas <drojas@moodlogic.com> wrote: template <class U> class P { public: template <class V> P(V * v) : u(v) {} private: U *u; }; Now "P<CMutex> p1(0);" will not compile but "P<CMutex> p1(new CMutex);" will. I believe this is due to the fact that in C++ it is illegal for the compiler to do more than one implicit cast. In this case it would have to implicitly cast from <int>(0) to <V *>(0) and then implicitly cast again from <V *>(0) to <U *>(0). Jon
participants (4)
-
Delfin Rojas
-
Jonathan Franklin
-
Ovanes Markarian
-
Zhuo Hao