Boost logo

Boost :

From: Ryan Gallagher (ryan.gallagher_at_[hidden])
Date: 2005-03-08 21:28:23


David Abrahams <dave <at> boost-consulting.com> writes:

>
> "Gennadiy Rozental" <gennadiy.rozental <at> thomson.com> writes:
>
> >> Agreed. I was going to say that an empty optional should print
> >> something, but couldn't articulate the reasons. Thanks.
> >
> > Printing something only for empty optional may lead to unclear output. For
> > example what should be an output of:
> >
> > optional<string> v = "empty";
> > optional<string> v;
> >
> > optional<bool> v = false;
> > optional<bool> v;
> >
> > optional<int> v = 0;
> > optional<int> v;
>
> Nothing.
>
> > Gennadiy
>
> But seriously, folks, any output is confusible. Just pick a
> sufficiently unlikely placeholder string.
>

How about using SML-like option-type notation:

SOME(empty)
SOME(false)
SOME(0)
NONE

so in general

template<typename T>
std::ostream&
operator<<(std::ostream& out, boost::optional<T> const& t_option)
{
   if (t_option) { out << "SOME(" << *t_option << ")"; }
   else { out << "NONE"; }

   return out;
}

-Ryan


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk