Streaming boost::optional yields inconsistent output in gcc 3.2.3
Hi, I defined the following operator to stream optional<char>'s: std::ostream & operator<< (std::ostream & os, boost::optional<char> const & op) { if (op) os << *op; else os << '_'; return os; } Now, when I use: boost::optional<char> a ('a'); std::cout << a; 'a' is printed like I expected. However, when I use: std::ostream_iterator<boost::optional<char> > oi (std::cout); *oi = a; '1' is printed and my operator is apparently ignored (I expected another 'a'). Is this a gcc (3.2.3) issue or a boost issue? And in any case: is there a workaround? Thanks, Eelis
Eelis van der Weegen wrote:
Hi,
I defined the following operator to stream optional<char>'s:
std::ostream & operator<< (std::ostream & os, boost::optional<char> const & op) { if (op) os << *op; else os << '_'; return os; }
Now, when I use:
boost::optional<char> a ('a'); std::cout << a;
'a' is printed like I expected. However, when I use:
std::ostream_iterator<boost::optional<char> > oi (std::cout); *oi = a;
'1' is printed and my operator is apparently ignored (I expected another 'a'). Is this a gcc (3.2.3) issue or a boost issue?
Neither. Your operator<< definition should be in namespace boost.
Peter Dimov wrote:
Eelis van der Weegen wrote:
Hi,
I defined the following operator to stream optional<char>'s:
std::ostream & operator<< (std::ostream & os, boost::optional<char> const & op) { if (op) os << *op; else os << '_'; return os; }
Now, when I use:
boost::optional<char> a ('a'); std::cout << a;
'a' is printed like I expected. However, when I use:
std::ostream_iterator<boost::optional<char> > oi (std::cout); *oi = a;
'1' is printed and my operator is apparently ignored (I expected another 'a'). Is this a gcc (3.2.3) issue or a boost issue?
Neither. Your operator<< definition should be in namespace boost.
Thanks a lot! For some reason I hadn't thought of potential namespace lookup issues but of course it makes perfect sense.
participants (2)
-
Eelis van der Weegen
-
Peter Dimov