[lexical_cast][variant] Compile failed when included boost.variant.
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
On 31 January 2012 14:43, Tianjiao Yin <ytj000@gmail.com> 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@lists.boost.org 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.
participants (2)
-
Tianjiao Yin
-
Vitaly Budovski