
On Wed, May 4, 2011 at 9:37 AM, Robert Jones <robertgbjones@gmail.com> wrote:
On Tue, May 3, 2011 at 8:52 PM, Olaf van der Spek <ml@vdspek.org> wrote:
On Tue, May 3, 2011 at 6:57 PM, Robert Jones <robertgbjones@gmail.com> wrote:
I'd prefer to see it return a boost::optional<T&>
What's the benefit?
Simply that the difficulty you're addressing is the possibility that there's no value to return. Boost.optional expresses that directly.
Returning NULL works just as well. Using a raw pointer also allows you to write string* ptr = find_ptr(). Using Boost Optional would make that longer.
What problem would that avoid? It'd require the user to do an additional check.
But you'd lose some flexibility. If you return a reference or copy of the pointed-to object you cannot then change the pointer itself, whereas if you return a reference to the pointer you could.
That's true, although I'm not sure that's a frequent use-case.
It's also a special case, which is best avoided if possible, that doesn't seem to offer any real benefit.
Containers of pointers occur quite frequently. There's even a Ptr Container lib. Olaf