I’m looking at [VC10’s] unique_ptr and they do a couple things I don’t understand:
typedef typename tr1::remove_reference<_Dx>::type _Dx_noref;
_Dx_noref& get_deleter()
{ // return reference to deleter
return (_Mydel);
}
unique_ptr(pointer _Ptr,
typename _If<tr1::is_reference<_Dx>::value, _Dx,
const typename tr1::remove_reference<_Dx>::type&>::_Type _Dt)
: _Mybase(_Ptr, _Dt)
{ // construct with pointer and (maybe const) deleter&
}
typename tr1::add_reference<_Ty>::type operator*() const
{ // return reference to object
return (*this->_Myptr);
}
Wouldn’t just writing _Dx& or _Ty& be the same thing?
I actually do understand why they did it here though:
unique_ptr(pointer _Ptr, typename tr1::remove_reference<_Dx>::type&& _Dt)
: _Mybase(_Ptr, _STD move(_Dt))
{ // construct by moving deleter
}
get_deleterAny reference is removed from the return type, then a reference is added back. In conformant C++11, adding a
&to an existing&(or&&) produces a&. In C++03 however, that would be forming a reference to reference type, which was illegal. Likely MSVC is using the old rules, or that code was written when it did and remains because it is harmless.constructor
Here they remove the reference, add
const, and then add the reference back, to be passing byconstreference. This is because addingconstdirectly to a reference type does nothing! (§8.3.2/1) In either C++11 or C++03, the parameter declaration would be valid but would not add aconst, if the reference weren’t removed and replaced.operator*This is essentially the same as
get_deleter, but they went about it a different way, and_Tycannot be a reference type to begin with. It looks to me like_Ty&would suffice, but it’s their prerogative.