Boost :: lexical_cast does not recognize an overloaded istream statement

I have the following code:

#include <iostream> #include <boost\lexical_cast.hpp> struct vec2_t { float x; float y; }; std::istream& operator>>(std::istream& istream, vec2_t& v) { istream >> vx >> vy; return istream; } int main() { auto v = boost::lexical_cast<vec2_t>("1231.2 152.9"); std::cout << vx << " " << vy; return 0; } 

I get the following compilation from Boost:

Error 1 error C2338: Target type is neither std :: istream able nor std::wistream able

It seems simple enough, and at the last hour I hit my head on the table. Any help would be appreciated!

EDIT: I am using Visual Studio 2013.

+5
source share
1 answer

In playback mode, 2-phase search.

You need to enable overloading using ADL, so lexical_cast will find it in the second phase.

So you have to move the overload to the mandala namespace

Here's a fully fixed example (you should also use std::skipws ):

Live on coliru

 #include <iostream> #include <boost/lexical_cast.hpp> namespace mandala { struct vec2_t { float x,y; }; } namespace mandala { std::istream& operator>>(std::istream& istream, vec2_t& v) { return istream >> std::skipws >> vx >> vy; } } int main() { auto v = boost::lexical_cast<mandala::vec2_t>("123.1 15.2"); std::cout << "Parsed: " << vx << ", " << vy << "\n"; } 

enter image description here

+7
source

Source: https://habr.com/ru/post/1207104/


All Articles