title: How to access attribute of spirit rule, if attribute types of rule and parser don't match?
body: <p>Using boost::spirit::qi for parsing, there's the possibility to use semantic actions to call functions via phoenix. The result can then be assigned to the rule's attribute, using <code>boost::qi::_val</code>, in case the attribute type of the rule and the assigned parser match. In case the types differ, the label <code>qi::_val</code> stands for the top attribute. This is the case in the following short working example, as the parsers for the rules <code>add</code> and <code>mul</code> return <code>tuple&lt;std::size_t, std::size_t&gt;</code>, while the rules themself expect <code>vector&lt;std::size_t&gt;</code>:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;boost/spirit/include/phoenix.hpp&gt;
  #include &lt;boost/spirit/include/qi.hpp&gt;
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  /* useful abbreviations */
  namespace ascii = boost::spirit::ascii;
  namespace ph = boost::phoenix;
  namespace qi = boost::spirit::qi;
  namespace ql = qi::labels;
  
  
  namespace parser
  {
  
  /* these functions are called by the semantic actions */
  std::vector&lt;std::size_t&gt; add(std::size_t a, std::size_t b)
  {
      return std::vector&lt;std::size_t&gt;{a, b, a+b};
  }
  
  std::vector&lt;std::size_t&gt; mul(std::size_t a, std::size_t b)
  {
      return std::vector&lt;std::size_t&gt;{a, b, a*b};
  }
  
  /* actual grammar */
  template&lt;typename Iterator&gt;
  class Parser : public boost::spirit::qi::grammar&lt;Iterator, std::vector&lt;std::size_t&gt;(), boost::spirit::ascii::space_type&gt;
  {
  public:
      Parser() : Parser::base_type(packed)
      {
          packed %= *(add | mul | val) &gt;&gt; qi::eoi;
          add = (qi::uint_ &gt;&gt; '+' &gt;&gt; qi::uint_ &gt;&gt; ';')[ qi::_val = ph::bind(&amp;parser::add, ql::_1, ql::_2) ];
          mul = (qi::uint_ &gt;&gt; '*' &gt;&gt; qi::uint_ &gt;&gt; ';')[ qi::_val = ph::bind(&amp;parser::mul, ql::_1, ql::_2) ];
          val %= qi::uint_ &gt;&gt; ';';
      }
  
  private:
      using Rule = boost::spirit::qi::rule&lt;Iterator, std::vector&lt;std::size_t&gt;(), boost::spirit::ascii::space_type&gt;;
      Rule packed;
      Rule add;
      Rule mul;
      Rule val;
  };
  
  } /* namespace parser */
  
  /* MAIN PROGRAM */
  int main()
  {
      using Iterator = std::string::const_iterator;
      parser::Parser&lt;Iterator&gt; parser;
      std::vector&lt;std::size_t&gt; result;
      
      std::string string = &quot;1; 2*4; 5; 6; 7+9; 10;&quot;;
      Iterator it = string.begin(), end = string.end();
      qi::phrase_parse(it, end, parser, ascii::space, result);
      
      std::cout &lt;&lt; &quot;[ &quot;;
      for(auto i: result) std::cout &lt;&lt; i &lt;&lt; &quot;, &quot;;
      std::cout &lt;&lt; &quot;]\n&quot;;
      
      return 0;
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>[ 7, 9, 16, 10, ]
  </code></pre>
  <p>The not-expected-but-really-wanted output would be:</p>
  <pre><code>[ 1, 2, 4, 8, 5, 6, 7, 9, 16, 10, ]
  </code></pre>
  <p>As you can see, the assignment <code>qi::_val = ph::bind(&amp;add, ql::_1, ql::_2)</code> simply overwrites everything in the top rules attribute. I know I can work around this, by appending to the top rules attribute, but I'd like to keep things clean and have this done by the parser of the rule <code>packed</code>.</p>
  <p>Is there a nice and easy way to write the result of the calls to <code>parser::add</code> and <code>parser::mul</code> to their respective rules' attributes?</p>