我正在编写的共享指针类中有一个方法。
template<class T>
template<class Y>
shared_ptr<T> &shared_ptr<T>::operator=(/*const*/ shared_ptr<Y> &r)
{
shared_ptr<T>(r).swap(*this);
return *this;
}
以这种方式使用时
class Foo
{
};
class Bar : public Foo
{
};
int main(int /*argc*/, char * /*argv*/[]) {
shared_ptr<Foo> giPtr(new Foo(1));
shared_ptr<Bar> giPtr2;
giPtr2 = glext::dynamic_pointer_cast<Foo>(giPtr);
}
在 MSVC 中生成以下错误:
1>c:\users\mehoggan\documents\github\x86-applications\glextensions\smart_ptrs\shared_ptr\shared_ptr.inl(53): error C2440: '<function-style-cast>' : cannot convert from 'glext::shared_ptr<T>' to 'glext::shared_ptr<T>'
1> with
1> [
1> T=Foo
1> ]
1> and
1> [
1> T=Bar
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1> main.cpp(28) : see reference to function template instantiation 'glext::shared_ptr<T> &glext::shared_ptr<T>::operator =<Foo>(glext::shared_ptr<Foo> &)' being compiled
1> with
1> [
1> T=Bar
1> ]
构造函数
template<class T>
shared_ptr<T>::shared_ptr() :
_px(0),
_pn()
{}
template<class T>
template<class Y>
shared_ptr<T>::shared_ptr(Y * p) :
_px(p),
_pn()
{
internal::shared_pointer_construct(this, p, _pn);
}
// TODO: Create shared ptr from weak pointer
template<class T>
shared_ptr<T>::shared_ptr(const shared_ptr &r) :
_px(r._px),
_pn(r._pn)
{}
template<class T>
template<class Y>
shared_ptr<T>::shared_ptr(const shared_ptr<Y> &r, element_type *p) :
_px(p),
_pn(r._pn)
{}
template<class T>
shared_ptr<T> &shared_ptr<T>::operator=(const shared_ptr<T> &r)
{
shared_ptr<T>(r).swap(*this);
return *this;
}
交换
template<class T>
void shared_ptr<T>::swap(shared_ptr<T> &other)
{
std::swap(_px, other._px);
_pn.swap(other._pn);
}