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

  • Home
  • SEARCH
  • 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 8874047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:37:24+00:00 2026-06-14T18:37:24+00:00

Suppose I have a std::string attribute, but for ease-of-parsing, I’d like to use qi::int_

  • 0

Suppose I have a std::string attribute, but for ease-of-parsing, I’d like to use qi::int_ or qi::double_.

Is there an easy way to do the conversion as a semantic action?

I tried something like this:

 std::stringstream ss; 
 my_int_as_str = qi::int_ [ ref(ss)<<_1; _val=ss.str() ];

but this wouldn’t even compile.

EDIT – attempt with sehe’s answer below

#include <iostream>
#include <string>
#include <vector>

#include <boost/spirit/include/qi.hpp>

namespace qi=boost::spirit::qi;
namespace phx=boost::phoenix;

int main(int argc, char* argv[])
{
    std::string test="123";
    std::string result;

    // 1. qi::raw[ qi::int_ ]       works
    // 2. qi::lexeme[ qi::int_ ]    doesn't
    // 3. qi::as_string[ qi::int_ ] doesn't
    qi::rule<std::string::const_iterator, std::string()> my_int_as_str = 
                   qi::raw[ qi::int_ ];
    parse( test.cbegin(), test.cend(), my_int_as_str, result );

    std::cout << result << std::endl;

    // -------------------------------------------------------------------------

    std::string test_vector="456 789";
    std::vector<std::string> result_vector;

    // 4. qi::raw[ qi::int_ ]       won't compile
    // 5. qi::lexeme[ qi::int_ ]    won't compile
    // 6. qi::as_string[ qi::int_ ] doesn't
    qi::rule<std::string::const_iterator,std::vector<std::string>(),qi::space_type> 
            my_int_as_str_vector = qi::lexeme[ qi::int_ ];

    phrase_parse(test_vector.cbegin(),test_vector.cend(),
            my_int_as_str_vector,qi::space,result_vector);

    for(auto& string: result_vector)
        std::cout << string << std::endl;

    return 0;
}
  • 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-06-14T18:37:25+00:00Added an answer on June 14, 2026 at 6:37 pm

    You can use attr_cast by specializing transform_attribute for your types.

    #include <iostream>
    #include <string>
    #include <vector>
    
    #include <boost/spirit/include/qi.hpp>
    
    namespace qi=boost::spirit::qi;
    namespace phx=boost::phoenix;
    
    
    namespace boost { namespace spirit { namespace traits
    {
    
        template <>
        struct transform_attribute<std::string, int, qi::domain>
        {
            typedef int type;
            static int pre(std::string& d) { return 0; }//not useful in this case but required to avoid compiler errors
            static void post(std::string& val, int const& attr) //`val` is the "returned" string, `attr` is what int_ parses
            {
                std::stringstream ss;
                ss << attr;
                val= ss.str();
            }
            static void fail(std::string&) {}
        };
    }}}
    
    int main(int argc, char* argv[])
    {
        std::string test="123";
        std::string test_vector="456 789";
    
        qi::rule<std::string::const_iterator,std::string()> my_int_as_str = qi::attr_cast(qi::int_);
        qi::rule<std::string::const_iterator,std::vector<std::string>(),qi::space_type> my_int_as_str_vector= *qi::attr_cast(qi::int_);
    
        std::string result;
        std::vector<std::string> result_vector;
    
        parse(test.cbegin(),test.cend(),my_int_as_str,result);
        phrase_parse(test_vector.cbegin(),test_vector.cend(),my_int_as_str_vector,qi::space,result_vector);
    
    
        std::cout << result << std::endl;
        for(auto& string: result_vector)
            std::cout << string << std::endl;
    
    
        return 0;
    }
    

    If you really need/want to use semantic actions the simplest alternative would be to define a function taking an int as argument and returning a string (this is simpler than the attr_cast alternative but it is also slower, nearly twice the time in my really simple benchmarks):

    std::string semantic_transform(int i)
    {
        std::stringstream ss;
        ss<<i;
        return ss.str();
    }
    ...
    std::string string_semantic;
    qi::parse(test.cbegin(),test.cend(),qi::int_[&semantic_transform],string_semantic);
    std::cout << string_semantic << std::endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a struct containing a std::string, like this: struct userdata{ int uid;
Suppose I have the following: struct Person { std::string mName; Birthday mBirthday; }; using
Suppose I have this function: std::string Func1(std::string myString) { //do some string processing std::string
For example, suppose I have a class: class Foo { public: std::string& Name() {
For example, suppose I have std::string containing UNIX-style path to some file: string path(/first/second/blah/myfile);
Suppose I have two containers of elements: std::vector<std::string> foo = { aa, bb, cc
Suppose I have a class Foo with a std::string member str . What should
Suppose I have method: void foo(const std::string& s); Can I create boost::function: boost::function<void(const std::string&)>
suppose I have a header foo.h like this: #ifndef FOO_H #define FOO_H #include <string>
Suppose I have the following snippets: int compareFoo(std::string** a, std::string** b) { return (**a).compare(**b);

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.