|
Boost : |
From: Gennadiy Rozental (gennadiy.rozental_at_[hidden])
Date: 2005-03-07 09:02:06
"Martin Bonner" <martin.bonner_at_[hidden]> wrote in message
news:E562FCEE3A42D61192880002A5FB433302D7A929_at_kite.pigroup.co.uk...
> ----Original Message----
> From: Gennadiy Rozental [mailto:gennadiy.rozental_at_[hidden]]
> Sent: 07 March 2005 07:18
> To: boost_at_[hidden]
> Subject: [boost] [optional]output
>
> > Hi,
> >
> > Maybe it's something that I missed, but why optional doesn't define
> > output operator something like this:
> >
> > template<typename T>
> > inline std::ostream&
> > operator<<( std::ostream& ostr, optional<T> const& v )
> > {
> > if( v )
> > ostr << *v;
> > return ostr;
> > }
>
> I don't think optional should define an output operator at all. There are
> too many possibly useful definitions. For example:
>
> template<typename T>
> inline std::ostream&
> operator<<( std::ostream& ostr, optional<T> const& v )
> {
> if( v )
> {
> ostr << true << ' ' << *v;
> }
> else
> {
> ostr << false;
> }
> return ostr;
> }
I believe this version is much less intuitive. optional<T> v is still value
of type T which may or may not be present. Accordingly when we print it we
print the value if it present or print nothing if it not. Anyone is free to
implement function for custom printing, but I believe default should behave
the way I describe above. BTW optional<T> currently is streamable , but it
prints 1 or 0 - initialized status - obviously bad choice for output
semantic.
Gennadiy
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk