Boost logo

Boost Users :

Subject: Re: [Boost-users] [Variant] stream output of ICU UnicodeString
From: Mostafa (mostafa_working_away_at_[hidden])
Date: 2011-10-10 08:46:16


On Sun, 09 Oct 2011 15:31:53 -0700, Gordon Woodhull <gordon_at_[hidden]>
wrote:

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

<snip>

>> 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.

Hmmm ... It seems the issue isn't with the placement of the calling code
in global namespace or not, but with the funny rule that unqualified name
lookup does not go beyond some ancestor scope if any possible match is
found in said ancestor. For example, even if the calling code in main() is
moved into function some_func in namespaces Detail::Huh, the name lookup
works (via unqualified name lookup in the global scope, before ADL kicks
in) for Bar::Foo. And it fails for Boost.Variant because there is some
possible match for operator<< associated with Boost.Variant which prevents
a lookup in the global scope. (At least according to:
http://stackoverflow.com/questions/4603886/c-operator-lookup-rules-koenig-lookup).

Mostafa


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