Re: naked pointer vs boost::shared_ptr<T>
Hi,
Daniel Kr?gler wrote:
<snip>
Where the former requires (and asserts) that its pointer never be null,
and the latter allows a null pointer, but asserts that a null pointer is
never dereferenced -- any feedback on this idea is appreciated :).
IMO, the name CheckedPointer is not very meaningful, especially
in contrast to OptionalPointer (which says more explicitely what
it wants). What about ValuePointer?
Yep, I think this is a good point. How about RequiredPointer<>?
One might also consider to seperate the concerns of holding and
preconditions by means of a proper policy. I remember some very
advanced ansatz (I would say it was from Andrei Alexandrescu),
that distinguished three poly domains.
I haven't used policies with these because they are supposed to be very
simple utility classes, without any sort of configuration.
If you're interested in the code, let me know.
Yes, would be nice to see.
This is what I have so far:
(CheckedPointer is roughly the same thing but with the asserts
uncommented). Comments/Suggestions welcome. Thanks!
---
// Util/OptionalPointer.hpp
#include <cassert>
namespace Util {
template <typename T>
class OptionalPointer {
T* P;
public:
inline OptionalPointer(T *const p = 0) : P(p) { /*assert(p);*/ }
inline OptionalPointer& operator = (T *const p) {
/*assert(p);
assert(P);*/
P = p;
return *this;
}
inline operator bool() const { return P != 0; }
inline T* get() const { assert(P); return P; }
inline T& operator* () const { return *get(); }
inline T* operator->() const { return get(); }
};
} // namespace Util.
---
Cheers,
-Al.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]