Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7524319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:04:10+00:00 2026-05-30T03:04:10+00:00

What’s the best approach to parsing std::string to some numeric type in C++, when

  • 0

What’s the best approach to parsing std::string to some numeric type in C++, when the target type isn’t known in advance?

I’ve looked at lexical_cast, but that takes the target type as a template parameter. I could write wrapper functions that abuse this by catching bad_lexical_cast and returning false, but that seems ugly.

My input values will typically be int or float and have extremely simple formatting, but something that’s flexible would be great!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:04:11+00:00Added an answer on May 30, 2026 at 3:04 am

    You could use either Boost Spirit Numerical Parsers or (ab)use Boost Lexicalcast.

    Boost Spirit allows you fine grained control of the format accepted, see e.g.

    • RealPolicies

    Here is a quick demo, that also shows how you could detect several possible numeric input formats (progressively) and return the type that was matched. Of course that could be overkill, but it should demonstrate how to use Spirit further.

    The demo also shows how to advance the input iterator so you can easily continue parsing where the numeric input ended.

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    namespace qi = boost::spirit::qi;
    
    enum numeric_types
    {
        fmt_none,
        fmt_float,
        fmt_double,
        fmt_uint,
        fmt_int,
        // fmt_hex, etc. 
    };
    
    template <typename It>
        bool is_numeric(It& f, It l, numeric_types& detected)
    {
        return qi::phrase_parse(f,l,
                qi::uint_   [ qi::_val = fmt_uint   ]
              | qi::int_    [ qi::_val = fmt_int    ]
              | qi::float_  [ qi::_val = fmt_float  ]
              | qi::double_ [ qi::_val = fmt_double ]
               ,qi::space, detected);
    }
    
    template <typename It>
        bool is_numeric(It& f, It l)
    {
        numeric_types detected = fmt_none;
        return is_numeric(f, l, detected);
    }
    
    int main()
    {
        const std::string input = "124, -25, 582";
        std::string::const_iterator it = input.begin();
    
        bool ok = is_numeric(it, input.end());
    
        if (ok)   
        {
            std::cout << "parse success\n";
            if (it!=input.end()) 
                std::cerr << "trailing unparsed: '" << std::string(it,input.end()) << "'\n";
        }
        else 
            std::cerr << "parse failed: '" << std::string(it,input.end()) << "'\n";
    
        return ok? 0 : 255;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.