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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:13:09+00:00 2026-06-04T14:13:09+00:00

I am new to boost spirit and I have the following problem: #include <string>

  • 0

I am new to boost spirit and I have the following problem:

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

#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_statement.hpp>

#include <boost/bind.hpp>

using namespace boost::spirit;
using namespace std;

struct MyGrammar
    : qi::grammar<string::const_iterator, string(), ascii::space_type> {
    MyGrammar();

    void myFun(const string& s);

    private:
    qi::rule<string::const_iterator, string(), ascii::space_type> myRule;
};



using namespace boost::spirit;
using namespace std;

MyGrammar::MyGrammar() : MyGrammar::base_type(myRule) {
    using qi::_1;

    myRule = int_ [boost::bind(&MyGrammar::myFun, this, _1)]; // fails
    myRule = int_ [_val = _1];  // fine
}

void MyGrammar::myFun(const string& s){
    cout << "read: " << s << endl;
}

int
main(){
}

With the first assignment of myRule I get compile errors while the second assignment compiles fine.

In the first case the compiler outputs huge error messages that I don’t understand.
At the end it says:

boost_1_49_0/include/boost/bind/bind.hpp:318:9: error: no match for call to '(const boost::_mfi::mf1<void, MyGrammar, const std::basic_string<char>&>) (MyGrammar* const&, const boost::phoenix::actor<boost::spirit::argument<0> >&)'
boost_1_49_0/include/boost/bind/mem_fn_template.hpp:163:7: note: candidates are: R boost::_mfi::mf1<R, T, A1>::operator()(T*, A1) const [with R = void, T = MyGrammar, A1 = const std::basic_string<char>&]
boost_1_49_0/include/boost/bind/mem_fn_template.hpp:184:7: note:                 R boost::_mfi::mf1<R, T, A1>::operator()(T&, A1) const [with R = void, T = MyGrammar, A1 = const std::basic_string<char>&]

Any ideas?
Thanks a lot for any help!

  • 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-04T14:13:11+00:00Added an answer on June 4, 2026 at 2:13 pm

    The first problem is that you specifiy std::string as your synthesized attribute, but then define your rule in terms of qi::int_, which has a synthesized attribute of int.

    The second problem is that, as the Spirit docs state directly, non-Phoenix functors take three arguments, not one:

    You can use Boost.Bind to bind member functions. For function objects, the allowed signatures are:

    void operator()(Attrib const&, unused_type, unused_type) const;
    void operator()(Attrib const&, Context&, unused_type) const;
    void operator()(Attrib const&, Context&, bool&) const;
    

    The third problem is that you’re using Spirit’s Phoenix _1 placeholder rather than boost::bind‘s placeholder (which is effectively in the global namespace).

    In summary, this should work:

    #include <string>
    #include <iostream>
    #include <boost/bind.hpp>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix_operator.hpp>
    #include <boost/spirit/include/phoenix_function.hpp>
    #include <boost/spirit/include/phoenix_statement.hpp>
    
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;
    
    struct MyGrammar :
        qi::grammar<std::string::const_iterator, int(), ascii::space_type>
    {
        MyGrammar();
        void myFun(int i, qi::unused_type, qi::unused_type);
    
    private:
        qi::rule<std::string::const_iterator, int(), ascii::space_type> myRule;
    };
    
    MyGrammar::MyGrammar() : MyGrammar::base_type(myRule)
    {
        myRule = qi::int_[boost::bind(&MyGrammar::myFun, this, _1, _2, _3)];
    }
    
    void MyGrammar::myFun(int const i, qi::unused_type, qi::unused_type)
    {
        std::cout << "read: " << i << '\n';
    }
    
    int main()
    {
        std::string const input = "42";
        std::string::const_iterator first = input.begin(), last = input.end();
        qi::phrase_parse(first, last, MyGrammar(), ascii::space);
    }
    

    That being said, unless you have a very specific reason to use boost::bind here, you should be using boost::phoenix::bind instead:

    #include <string>
    #include <iostream>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix_operator.hpp>
    #include <boost/spirit/include/phoenix_function.hpp>
    #include <boost/spirit/include/phoenix_statement.hpp>
    #include <boost/spirit/include/phoenix_bind.hpp>
    
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;
    
    struct MyGrammar :
        qi::grammar<std::string::const_iterator, int(), ascii::space_type>
    {
        MyGrammar();
        void myFun(int i);
    
    private:
        qi::rule<std::string::const_iterator, int(), ascii::space_type> myRule;
    };
    
    MyGrammar::MyGrammar() : MyGrammar::base_type(myRule)
    {
        myRule = qi::int_[boost::phoenix::bind(&MyGrammar::myFun, this, qi::_1)];
    }
    
    void MyGrammar::myFun(int const i)
    {
        std::cout << "read: " << i << '\n';
    }
    
    int main()
    {
        std::string const input = "42";
        std::string::const_iterator first = input.begin(), last = input.end();
        qi::phrase_parse(first, last, MyGrammar(), ascii::space);
    }
    

    This allows your bound member function to take only a single argument – the synthesized attribute – as you originally wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new in using boost and have a problem. I need shared_mutex function in
Code: #include <iostream> #include stdafx.h #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> using namespace std; boost::mutex mut;
I'm new to boost::spirit. I've stumbled over a simple thing. Given a string like
I'm new to Boost Spirit and trying to write JSON parser using Boost Spirit
I'm trying to parse JSON string using Boost Spirit store JSON object into recursive
I have the following spirit grammar. I am trying to create a vector of
I am creating a new boost::thread using boost::bind , and storing it in a
So I have such log class: #include <iostream> #include <sstream> #include <boost/circular_buffer.hpp> #include <boost/foreach.hpp>
I'm very new to boost::spirit/fusion. Could someone please explain to me why the following
What are the differences in using boost:thread, Posix Thread library and the new C++11

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.