I am learning shared_ptr in c++.
I have learned that I can check whether a shared_ptr has nullptr or not inside if sentence like
std::shared_ptr<int> sptr;
if ( sptr ) {
std::cout << "sptr has a non-null pointer." << std::endl;
} else {
std::cout << "sptr has a null pointer." << std::endl;
}
Now what I would like to do now is
bool NullCheck(std::shared_ptr<int>& sptr) {
return sptr; // I hoped that this works like inside if sentence.
}
However it does not work. Compiler tells me it cannot convert the shared_ptr to bool.
I can check whether the shared_ptr has nullptr or non-nullptr by the explicit comparison but I would like to know the way like inside if sentence. Someone who knows it is possible or not, please tell me.