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 9166113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:05:01+00:00 2026-06-17T15:05:01+00:00

I have a rule which is supposed to return a Boost.Fusion ASSOC_STRUCT. I am

  • 0

I have a rule which is supposed to return a Boost.Fusion ASSOC_STRUCT. I am trying to assign to _val the results parsed by the rule’s parsers, but I cannot make it work. I will skip the talking and present you directly with the relevant code.

#include <boost/fusion/include/define_assoc_struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/include/as_vector.hpp>
#include <boost/spirit/include/qi_symbols.hpp>
#include <iostream>
#include <string>

namespace keys {
   struct actor_key;
   struct action_key;
   struct money_key;
 }

 BOOST_FUSION_DEFINE_ASSOC_STRUCT(
    (), ActionLine,
    (int, action, keys::action_key)
    (int, amount, keys::money_key)
    (int, actor, keys::actor_key)
  )

int main ()
{
  namespace qi = boost::spirit::qi;
  using qi::_1;
  using qi::_2;
  using qi::_a;
  using qi::int_;
  using qi::lit;
  using boost::spirit::_val;
  using boost::fusion::as_vector;

  qi::symbols<char, int> name_parser_;
  name_parser_.add("name4", 4);

  std::string input("string1 ($7) string2 name4");
  std::string::const_iterator f(input.begin()), l(input.end());  
  ActionLine expected{0, 7, 4}, result;

  //The following rule is supposed to parse input of the form
  //"string1 ($[integer]) string2 [name]"
  //and return a triple (ActionLine) with the values { 0, value of (int_), value of (name_parser_) }
  qi::rule<std::string::const_iterator, ActionLine()> action_line_ =
     lit("string1 ($") >> int_ >> lit(") string2 ") >> name_parser_
     // [ _val = ActionLine{0, _1, _2} ]; // this is what I am trying to achieve
    [ _val = ActionLine{0, 7, 4} ]; //this compiles, but of course is not what I need

   bool b =
     qi::parse(f, l, action_line_, result) &&
     as_vector(result) == as_vector(expected);

   std::cout << "test: " << std::boolalpha << b << std::endl;
   return 0;
 }

(Compile with g++ above_file.cpp -std=c++0x)
Compiler error is somewhat different in my real application than in this example, but it is something like (in the line of _val = ActionLine{0, _1, _2} ):
No matching function of call ::ActionLine::ActionLine(), and I guess it cannot convert _1 and _2 to ints.

I also tried to add local int variables and use them to copy the parsed values, but it did not work, not did using boost::phoenix::at(_1,0), boost::phoenix::at(_1,1) ( I found those ideas here boost spirit semantic action parameters)

  • 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-17T15:05:02+00:00Added an answer on June 17, 2026 at 3:05 pm

    You need to use phoenix::construct in your semantic action to do what you want.

    #include <boost/fusion/include/define_assoc_struct.hpp>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix_core.hpp>
    #include <boost/spirit/include/phoenix_operator.hpp>
    #include <boost/spirit/include/phoenix_object.hpp> //CHANGE:you need to include this to use "phoenix::construct"
    #include <boost/fusion/include/as_vector.hpp>
    #include <boost/spirit/include/qi_symbols.hpp>
    #include <iostream>
    #include <string>
    
    namespace keys {
       struct actor_key;
       struct action_key;
       struct money_key;
     }
    
     BOOST_FUSION_DEFINE_ASSOC_STRUCT(
        (), ActionLine,
        (int, action, keys::action_key)
        (int, amount, keys::money_key)
        (int, actor, keys::actor_key)
      )
    
    int main ()
    {
      namespace qi = boost::spirit::qi;
      namespace phx = boost::phoenix;
      using qi::_1;
      using qi::_2;
      using qi::_a;
      using qi::int_;
      using qi::lit;
      using boost::spirit::_val;
      using boost::fusion::as_vector;
    
      qi::symbols<char, int> name_parser_;
      name_parser_.add("name4", 4);
    
      std::string input("string1 ($7) string2 name4");
      std::string::const_iterator f(input.begin()), l(input.end());  
      ActionLine expected{0, 7, 4}, result;
    
      //The following rule is supposed to parse input of the form
      //"string1 ($[integer]) string2 [name]"
      //and return a triple (ActionLine) with the values { 0, value of (int_), value of (name_parser_) }
      qi::rule<std::string::const_iterator, ActionLine()> action_line_ =
         (lit("string1 ($") >> int_ >> lit(") string2 ") >> name_parser_) //CHANGE:the parentheses are important otherwise the semantic action would be attached to name_parser_
        [ _val = phx::construct<ActionLine>(0, _1, _2) ]; //CHANGE: you need to use phoenix to use the placeholders _1, _2, etc
    
       bool b =
         qi::parse(f, l, action_line_, result) &&
         as_vector(result) == as_vector(expected);
    
       std::cout << "test: " << std::boolalpha << b << std::endl;
       std::cout << at_key<keys::action_key>(result)<< ", " << at_key<keys::money_key>(result)<< ", " << at_key<keys::actor_key>(result) << std::endl;
    
       return 0;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the rule below which works perfectly, however My oldsite urls have .html
I have a make rule which generates a dependencies file for a list of
I have a free shipping price rule which is configured like this: All customer
I have a project in which the maven-enforcer rule fails with a multi-module build
I don't really know what's causing this problem but my program, which is supposed
I have a good amount of code which looks like this: rule requestIntHoleTemplate {
In my .htaccess file I have this rewrite rule: RewriteRule example actual-page.php [L] Which
I have rewrite rule which works on subdomain other than www. I want to
I have rule configuration on UI which builds to Java POJO. How could I
I have following Drools rule to which I send map filled with element ,

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.