Consider the following code:
std::auto_ptr<std::string> p;
if (p.get() == 0) {
...
}
Is the get() member function a standard and reliable way for checking that p has not been initialized? Will it always return 0, irrespective of the platform, compiler, compiler's optimization flags, etc.?
The
getmethod ofauto_ptrhas no preconditions.That means, it is always safe to call that method, regardless of what state the
auto_ptrobject is in.Contrast this with the
operator*member function, which does have a precondition ofget() != 0. The C++ Standard specifies preconditions for member functions in a Requires clause. If no such clause is present for a function, it is always safe to call.