I had read from some blog that a default-constructed (empty) shared_ptr
is automatically initialized to nullptr
. But could not find any such explicit statement in the statndards.
I wrote a small snippet (Linux Compiled) to confirm this:
#include <iostream>
#include <memory>
struct Base;
int main()
{
std::shared_ptr<Base> p;
Base* b;
if (p == nullptr) {
std::cout << "p IS NULL \n";
}
else {
std::cout << "p NOT NULL \n";
}
if (b == nullptr) {
std::cout << "b IS NULL \n";
}
else {
std::cout << "b NOT NULL \n";
}
return 0;
}
Output:
p IS NULL b NOT NULL
From this I see that the smart pointers are implicitly assigned nullptr
at the time of Declaration. Can somebody confirm this behavior? Is it safe to use a shared_ptr
without manually assigning a nullptr
to it?