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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:12:30+00:00 2026-06-04T07:12:30+00:00

In Boost::Spirit, how can I trigger an expectation_failure from a function bound with Boost::Bind

  • 0

In Boost::Spirit, how can I trigger an expectation_failure from a function bound with Boost::Bind?

Background: I parse a large file that contains complex entries. When an entry is inconsistent with a previous entry I want to fail and throw an expectation_failure (containing proper parse position information). When I parse an entry I bind a function that decides if the entry is inconsistent with something seen before.

I made up a little toy example that shows the point. Here I simply want to throw an expectation_failure when the int is not divisible by 10:

#include <iostream>
#include <iomanip>
#include <boost/spirit/include/qi.hpp>
#include <boost/bind.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp>
namespace qi = boost::spirit::qi;
namespace classic = boost::spirit::classic;

void checkNum(int const& i) {
  if (i % 10 != 0) // >> How to throw proper expectation_failure? <<
    std::cerr << "ERROR: Number check failed" << std::endl;
}

template <typename Iterator, typename Skipper>
struct MyGrammar : qi::grammar<Iterator, int(), Skipper> {
  MyGrammar() : MyGrammar::base_type(start) {
    start %= qi::eps > qi::int_[boost::bind(&checkNum, _1)];
  }
  qi::rule<Iterator, int(), Skipper> start;
};

template<class PosIter>
std::string errorMsg(PosIter const& iter) {
  const classic::file_position_base<std::string>& pos = iter.get_position();
  std::stringstream msg;
  msg << "parse error at file " << pos.file
      << " line " << pos.line << " column " << pos.column << std::endl
      << "'" << iter.get_currentline() << "'" << std::endl
      << std::setw(pos.column) << " " << "^- here";
  return msg.str();
}

int main() {
  std::string in = "11";
  typedef std::string::const_iterator Iter;
  typedef classic::position_iterator2<Iter> PosIter;
  MyGrammar<PosIter, qi::space_type> grm;
  int i;
  PosIter it(in.begin(), in.end(), "<string>");
  PosIter end;
  try {
    qi::phrase_parse(it, end, grm, qi::space, i);
    if (it != end)
      throw std::runtime_error(errorMsg(it));
  } catch(const qi::expectation_failure<PosIter>& e) {
    throw std::runtime_error(errorMsg(e.first));
  }
  return 0;
}

Throwing an expectation_failure would mean that I get an error message like this on an int that is not divisible by 10:

parse error at file <string> line 1 column 2
'11'
  ^- here
  • 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-04T07:12:31+00:00Added an answer on June 4, 2026 at 7:12 am

    You can use the _pass placeholder in phoenix to enforce a parse fail. Something like this should work.

    bool myfunc(int i) {return i%10 == 0;}
    
    ...
    _int [ _pass = phoenix::bind(myfunc,_1)] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can Spirit (part of Boost C++ library) be used to parse out binary data
I have large Boost/Spirit metaprogram that is blowing gcc's stack when I try to
I want to parse a file into an AST using Boost Spirit. The root
I remember that when using Boost.Spirit and for the std::function addition to C++0x, you
I have to use boost::spirit for parsing, and I want use phrase_parse function :
How does one use boost::spirit with an input that consists of something other than
I'm trying to learn boost::spirit . As an example, I'm trying to parse a
In several questions I've seen recommendations for the Spirit parser-generator framework from boost.org ,
Recently, I try to use the boost::spirit::qi binary endian parser to parse some binary
i found this in this file: http://www.boost.org/doc/libs/1_43_0/boost/spirit/home/phoenix/core/actor.hpp What does this syntax means? struct actor

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.