The Title pretty much sums up my question. Why can’t the following be done to check for a null pointer?
auto_ptr<char> p( some_expression );
// ...
if ( !p ) // error
This must be done instead:
if ( !p.get() ) // OK
Why doesn’t auto_ptr<T> simply have operator!() defined?
Seems to be there was an error in its design. This will be fixed in C++0x.
unique_ptr(replacement forauto_ptr) containsexplicit operator bool() const;Quote from new C++ Standard:
Some clarification:
Q: What’s wrong with
a.get() == 0?A: Nothing is wrong with
a.get()==0, but smart pointers lets you work with them as they were real pointers. Additionaloperator bool()gives you such a choice. I think, that the real reason for makingauto_ptrdeprecated is that is has has not intuitive design. Butoperator boolforunique_ptrin the new Standard means that there are no reasons not to have it.