lundi 26 janvier 2015

Looking at std::unique_ptr and its nullptr_t constructor

I am trying to understand why unique_ptr has a nullptr_t constructor



constexpr unique_ptr::unique_ptr( nullptr_t );


I had assumed this was because the normal one argument constructor was explicit and thus would reject the nullptr value:



explicit unique_ptr::unique_ptr( pointer p );


But when I build an example it compiler fine:



namespace ThorsAnvil
{
template<typename T>
class SmartPointer
{
public:
SmartPointer() {}
explicit SmartPointer(T*){}
};
}


template<typename T>
using SP = ThorsAnvil::SmartPointer<T>;
int main()
{

SP<int> data1;
SP<int> data2(new int); // fine
SP<int> data3(nullptr); // fine
}


Here is the output:



> g++ --version
Configured with: --prefix=/Applications/http://ift.tt/1d5DwEL --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
> g++ -Wall -Wextra -std=c++11 SP1.cpp


Why does std::unique_ptr need the extra constructor that takes a nullptr_t argument?


Aucun commentaire:

Enregistrer un commentaire