c++ - Why does unique_ptr overload reset(pointer p = pointer()) and reset(nullptr_t)? -
accroding http://en.cppreference.com/w/cpp/memory/unique_ptr/reset,
void reset( pointer ptr = pointer() ); template< class u > void reset( u ) = delete; void reset( std::nullptr_t p ); 1) given current_ptr, pointer managed *this, performs next actions, in order: saves re-create of current pointer old_ptr = current_ptr; overwrites current pointer argument current_ptr = ptr; if old pointer non-empty, deletes managed object if(old_ptr != nullptr) get_deleter()(old_ptr).
2) in specialization dynamic arrays, std::unique_ptr<t[]>, template fellow member provided prevent using reset() pointer derived (which result in undefined behavior arrays).
3) in specialization dynamic arrays, std::unique_ptr<t[]>, 3rd overload necessary allow reset nullptr (which otherwise prohibited template overload). equivalent reset(pointer())
now reset(nullptr) equivalent reset(pointer()), why latter exist?
if want reset array form unique_ptr, why can not utilize rest(pointer())?
the
template< class u > void reset( u ) = delete; would chosen phone call nullptr argument, if not for
void reset( std::nullptr_t p ); that's why exists, allow phone call nullptr.
example (compile fix defined suppress compilation error):
#include <cstddef> // std::nullptr_t struct s { void reset( char* ) {} template< class type > void reset( type ) = delete; #if prepare void reset( std::nullptr_t ) {} #endif }; auto main() -> int { s().reset( nullptr ); // fails when prepare not defined. } c++ arrays c++11 overloading unique-ptr
No comments:
Post a Comment