
"Martin Bonner" <martin.bonner@pitechnology.com> wrote in message news:E562FCEE3A42D61192880002A5FB433302D7A929@kite.pigroup.co.uk...
----Original Message---- From: Gennadiy Rozental [mailto:gennadiy.rozental@thomson.com] Sent: 07 March 2005 07:18 To: boost@lists.boost.org 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