Your problem is that boost::lexical_cast does not ignore spaces in the input (it disables the skipws flag of the input stream).
The solution is to either set the flag yourself in your extract statement, or simply skip one character. Indeed, the extraction operator should reflect the insertion operator: since you explicitly put a space in the output of the Test instance, you must explicitly read the space in the extraction of the instance.
This thread discusses the topic, and the recommended solution is to do the following:
friend std::istream& operator>>(std::istream &input, Test &test) { input >> test.a; if((input.flags() & std::ios_base::skipws) == 0) { char whitespace; input >> whitespace; } return input >> test.b; }
Luc touraille
source share