• Advertisement
Sign in to follow this  

boost::spirit float parser

This topic is 3499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need a floating point parser (not double) so following the docs I have done this:
std::vector<float> items;
std::string line = " 1234.1234 343.32" //in real code, it's a line read in from a file.
real_parser<float,real_parser_policies<float>> float_p;

parse(line.begin(),line.end(),
				//BEGIN GRAMMAR
				*space_p >> float_p[push_back_a(items)] >> *(+space_p >> float_p[push_back_a(items)]) >> *space_p
				//END GRAMMAR
				);



It works, however I get this compile warning:
C:\Program Files\boost\boost_1_35_0\boost/spirit/core/primitives/impl/numerics.ipp(89) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>        C:\Program Files\boost\boost_1_35_0\boost/spirit/core/primitives
/impl/numerics.ipp(241) : see reference to function template instantiation 
'bool boost::spirit::impl::radix_traits<10>::digit<char,T>(CharT,T &)' being 
compiled
1>        with
1>        [
1>            T=float,
1>            CharT=char
1>        ]
.... truncated b/c TLDR



I've had it beaten into me that warning = bad, and would therefore like to resolve this.

Share this post


Link to post
Share on other sites
Advertisement
I don't know if you pasted the code or wrote it from memory but >> is not valid, it will _always_ be interpreted as a bit shift. You need to but spacing between them like:

real_parser< float,real_parser_policies<float> > float_p;

And why not just use boost::spirit::real_p instead? Sure, you may get a cast from double to float, but that may be optimized away anyways.

Share this post


Link to post
Share on other sites
Quote:
Original post by lexs
And why not just use boost::spirit::real_p instead? Sure, you may get a cast from double to float, but that may be optimized away anyways.

Well I was trying to get rid of the cast - I'm not a fan of downcasting from double to float.

Share this post


Link to post
Share on other sites
Quote:
I don't know if you pasted the code or wrote it from memory but >> is not valid, it will _always_ be interpreted as a bit shift. You need to but spacing between them like:
It's valid as of TR1, and MSVC2005.

Share this post


Link to post
Share on other sites
Quote:
Original post by Deyja
Quote:
I don't know if you pasted the code or wrote it from memory but >> is not valid, it will _always_ be interpreted as a bit shift. You need to but spacing between them like:
It's valid as of TR1, and MSVC2005.

It's not part of TR1. lexs is, strictly speaking, correct WRT the C++03 standard, although it is admittedly available as a Microsoft extension in their compilers (which cannot be disabled via /Za in 2008, sadly).

Share this post


Link to post
Share on other sites
Quote:
Original post by lexs
I don't know if you pasted the code or wrote it from memory...

memory.

Quote:
Sure, you may get a cast from double to float,

is there way to do an explicit cast here to get rid of the warning? I couldn't figure where the hell the cast should go though!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement