Boost logo

Boost Users :

Subject: Re: [Boost-users] ambiguous overload for operator>> error when using lexical_cast with qvm::vec
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2016-09-28 16:39:51


2016-09-17 12:20 GMT+03:00 kique G <kique.g_at_[hidden]>:
> Hi,
>
> I am trying to use lexical_cast to convert from strings to qvm vectors, but
> I keep getting an ambiguous operator>> error. I have overloaded the >>
> operator for istreams and that seems to be working fine when I use it to
> read from a istringstream. In a lexical_cast, though, I get the "ambiguous
> overload for operator>>" error. I have tried declaring my operator>>
> overload inside the std and boost::qvm namespaces to check if it was
> something related to ADL, but I keep getting the same error. The thing is
> the other candidates do not seem to make a lot of sense to me. Could
> somebody please tell me what I am doing wrong?
>
> Thanks in advance,
>
> Enrique Garcia
>
> This is a minimal example (I am building gcc/boost from the last versions on
> their repositories):
>
> #include <iostream>
> #include <boost/qvm/vec.hpp>
> #include <boost/lexical_cast.hpp>
>
> std::ostream& operator<<(std::ostream& out, boost::qvm::vec<float, 3> const&
> v)
> {
> out << "(" << v.a[0] << "," << v.a[1] << "," << v.a[2] << ")";
> }
>
> std::istream& operator>>(std::istream& in, boost::qvm::vec<float, 3>& v)
> {
> char c;
> return in >> c >> v.a[0] >> c >> v.a[1] >> c >> v.a[2] >> c;
> }
>
> int main()
> {
> boost::qvm::vec<float, 3> vect { 1.0f, 2.0f, 3.0f };
> std::cout << vect << std::endl;
>
> // This code works just fine. (4.0,5.0,6.0) is printed
> std::istringstream istream("(4.0,5.0,6.0");
> istream >> vect;
> std::cout << vect << std::endl;
>
> // This code generates an ambiguous overload for operator>> error (see
> below)
> vect = boost::lexical_cast<boost::qvm::vec<float, 3>>("(1.0,2.0,3.0)");
> std::cout << vect << std::endl;
>
> return 0;
> }

You have defined the operator << and operator>> in global namespace
instead of namespace boost::qvm. So those operators could not be found
using ADL. Try adding `namespace boost { namespace qvm {` and `}}`
around them and everything must work well.

Note that defining anything in boost::* namespace is not portable, not
recommended and will definitely break some time. Take a closer look at
the Boost.QVM and fill a feature request if QVM does not have istream
operators.

-- 
Best regards,
Antony Polukhin

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