I was trying to understand how the weak pointer member (using the visual studio 2013 C++ compiler)
mutable weak_ptr<_Ty> _Wptr
from the template class enable_shared_from_this
was initialized when a std::shared_ptr<_Ty>
was created out of an object that derived from it. For this purpose I traced the code from a simple code that created a std::shared_ptr<_Ty>
with the template function std::make_share<_Ty>
I found that the initialization of _Wptr
proceeded when the compiler called the method _Enable_shared
within the base class _Ref_count_del_alloc
.
Now 2 template methods were defined for this purpose. One method is defined when the _Ty
class is derived from enable_shared_from_this
. This is the method:
template<class _Ty>
inline void _Enable_shared(_Ty *_Ptr, _Ref_count_base *_Refptr,
typename _Ty::_EStype * = 0)
{ // reset internal weak pointer
if (_Ptr)
_Do_enable(_Ptr,
(enable_shared_from_this<typename _Ty::_EStype>*)_Ptr, _Refptr);
}
The second method is defined when the _Ty
class is not derived from enable_shared_from_this
. This is the method:
inline void _Enable_shared(const volatile void *, const volatile void *)
{
// not derived from enable_shared_from_this; do nothing
}
The question I have is how the C++ compiler is able to resolve which method to use? Beside I would have expected both methods to be instantiated at compile time and have a compile error if the class is not derived from enable_shared_from_this
. Yet the compiler seems to select only one method to instantiate and the appropriate method.
Aucun commentaire:
Enregistrer un commentaire