Boost logo

Boost Users :

Subject: Re: [Boost-users] [lexical_cast][variant] Compile failed when included boost.variant.
From: Vitaly Budovski (vbudovski+news_at_[hidden])
Date: 2012-01-31 00:00:04


On 31 January 2012 14:43, Tianjiao Yin <ytj000_at_[hidden]> wrote:
> Hello, all:
>
> I met a strange compile error, this code will reproduce the problem::
>
>    #include <boost/lexical_cast.hpp>
>    #include <boost/variant.hpp>
>    #include <iostream>
>
>    namespace foo {
>    struct bar {};
>    }
>
>    std::ostream& operator << (std::ostream& out, foo::bar) {
>        return out << "BAR";
>    }
>
>    int main() {
>        boost::lexical_cast<std::string>(foo::bar());
>    }
>
> However, if I just remove that unused include boost.variant, it works::
>
>    #include <boost/lexical_cast.hpp>
>    //#include <boost/variant.hpp>
>    #include <iostream>
>
>    namespace foo {
>    struct bar {};
>    }
>
>    std::ostream& operator << (std::ostream& out, foo::bar) {
>        return out << "BAR";
>    }
>
>    int main() {
>        boost::lexical_cast<std::string>(foo::bar());
>    }
>
> Another solution is put that "operator<<" in namespace foo::
>
>    #include <boost/lexical_cast.hpp>
>    #include <boost/variant.hpp>
>    #include <iostream>
>
>    namespace foo {
>    struct bar {};
>    std::ostream& operator << (std::ostream& out, foo::bar) {
>        return out << "BAR";
>    }
>    } // namespace foo
>
>    int main() {
>        boost::lexical_cast<std::string>(foo::bar());
>    }
>
> Anyway, what is boost.variant's business here?
> Won't that fail to compile, or am I missing something?
> BTW, I'm using boost 1.48.0 and gcc 4.6.2 in Linux 3.2.2.
>
> Regards,
> Tianjiao
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users

Your overloaded operator isn't found by ADL, which looks for it in
namespaces std and foo. You should put it in the same namespace as
your struct.


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