Boost logo

Boost Users :

From: Delfin Rojas (drojas_at_[hidden])
Date: 2006-03-15 19:05:52


> François Duranleau wrote:
>
> If I read the documentation correctly about
> boost::lexical_cast, for a convertion between two types to
> work, the source type must be OutputStreamable and the target
> type must be InputStreamable (plus some other requirements
> about copying and default construction).
>

The problem with your example was that after you read the first double there
was a blank space in the input so you could not read the second double.
Something like this will do:

istream& operator >> ( istream& in , vec& v ) {
     for ( int i = 0 ; i < vec::size ; ++ i )
         if ((in >> v.data[ i ]) && !in.eof())
             in.get();
     return in ;
}

I ran into another little problem while debugging your example so I thought
I would mention it. Although the documentation does not explicitly say so
you kind of make sense that lexical_cast expects the destination object's
operator>> to use the complete output produced by the source object's
operator<<. In other words:

stream << source
stream >> target
!!! stream.eof() must be true at this point

So if you try (like I did) lexical_cast<vec>("1 2 3") with vec.size = 2 it
will fail.

Hope this helps,

-delfin


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