# boost::spirit float parser

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

## 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
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 on other sites
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 on other sites
Quote:
 Original post by lexsAnd 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 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 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 on other sites
Quote:
 Original post by lexsI 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!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634150
• Total Posts
3015837
×