Boost logo

Boost Users :

Subject: Re: [Boost-users] [Variant] stream output of ICU UnicodeString
From: Gordon Woodhull (gordon_at_[hidden])
Date: 2011-10-09 18:31:53


On Oct 9, 2011, at 3:12 PM, Mostafa <mostafa_working_away_at_[hidden]> wrote:

> On Sun, 09 Oct 2011 06:53:22 -0700, André <andre_at_[hidden]> wrote:
>
>> Hi,
>> I'm trying to stream a Boost::Variant with << to std::cout but when I include an UnicodeString member from the ICU Unicode library the compiler doesn't recognices my overloaded operator<< for the UnicodeString.
>>
>> Here is a test program:
>
> <snip>
>
>>
>> I'm using gcc 4.4.5 on Ubuntu 11.04 and boost 1.42.
>> Does someone know why the overloaded operator isn't used or how I can get the test program to work?
>
> Keeping in mind that UnicodeString is defined in namespace icu_<some-version-number>, what you've pointed out is not limited to UnicodeString. The following simplified example apparently shows ADL failing for Boost.Variant when the streaming operator for Foo is defined in the global namespace instead of Foo's defining namespace. (gcc 4.0.1, boost 1.4.5)
>
> #include <iostream>
> #include <boost/variant.hpp>
>
> namespace Bar
> {
> struct Foo {};
> }
>
> std::ostream& operator<<(std::ostream& stream, Bar::Foo const &b) {
> return stream << "Foo";
> }
>
> int main() {
>
> //Not compiles.
> boost::variant<Bar::Foo, char const *, int, double> test;
>
> //Compiles.
> //Bar::Foo test;
>
> test = Bar::Foo();
>
> std::cout << test << std::endl;
>
> return 0;
> }
>
> Is this a bug in Boost.Variant?

IIUC operators can only be found reliably by ADL and must always be defined in an associated namespace. It will only work to put them in the global namespace if the calling code (in this case, variant serialization) were also in the global namespace, which wouldn't be good.

Cheers,
Gordon


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net