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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:45:35+00:00 2026-06-16T07:45:35+00:00

So what I am trying to do is to parse a list of strings:

  • 0

So what I am trying to do is to parse a list of strings:

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

std::string TEST = "aa\nbbbb\nccc\n";

std::istringstream INPUT (TEST);
std::noskipws(INPUT);

typedef std::istreambuf_iterator<char> base_iterator;
typedef boost::spirit::multi_pass<base_iterator>  multi_pass_iter;
typedef boost::spirit::classic::position_iterator2<multi_pass_iter> pos_iterator;

base_iterator base_begin(INPUT);

multi_pass_iter first =  boost::spirit::make_default_multi_pass(base_begin);
multi_pass_iter last;

pos_iterator pfirst(first,last,std::string("DD"));
pos_iterator plast;

using qi::lexeme;
using ascii::alpha;

std::vector<std::string> DDD;
bool res = qi::phrase_parse(pfirst,plast,* lexeme[+alpha],ascii::space,DDD);

for (const auto & d : DDD) std::cout << d << " (" << d.size() << ")" << std::endl;

What i get in DDD are 3 strings of the correct size, but all of whitespaces.

If instead i use

bool res = qi::phrase_parse(first,last,* lexeme[+alpha],ascii::space,DDD);

everything works as expected.
I used position_iterator2 in the past without any problem, so I don’t believe it is a bug. Am I missing something?

  • 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-16T07:45:36+00:00Added an answer on June 16, 2026 at 7:45 am

    There is an example here that doesn’t work either. Using Visual Studio 2012 both give a warning:

    boost/iterator/iterator_adaptor.hpp(306): warning C4172: returning address of local variable or temporary
    boost_1_52_0\boost/iterator/iterator_adaptor.hpp(306) : while compiling class template member function 'const char &boost::iterator_adaptor<Derived,Base,Value,Traversal>::dereference(void) const'
             with
             [
                Derived=boost::spirit::classic::position_iterator2<forward_iterator_type>,
                Base=forward_iterator_type,
                Value=const char,
                Traversal=boost::forward_traversal_tag
             ]
    

    A quick google search of “iterator_adaptor dereference temporary” leads to this that recommends that the Reference parameter of iterator_adaptor be a non-reference type.

    In order to accomplish that you need to change the file “boost/spirit/home/classic/iterator/impl/position_iterator.ipp”. Specifically you’d need to change:

    typedef boost::iterator_adaptor<
        main_iter_t,
        ForwardIterT,
        const_value_type,
        boost::forward_traversal_tag
    > type;
    

    to:

    typedef boost::iterator_adaptor<
        main_iter_t,
        ForwardIterT,
        const_value_type,
        boost::forward_traversal_tag,
        const_value_type
    > type;
    

    this leads to a new error in both g++ and vc11:

    boost_1_52_0\boost/concept_check.hpp(212): error C2440: 'initializing' : cannot convert from 'boost::detail::iterator_category_with_traversal<Category,Traversal>' to 'std::forward_iterator_tag'
              with
              [
                  Category=std::input_iterator_tag,
                  Traversal=boost::forward_traversal_tag
              ]
              No constructor could take the source type, or constructor overload resolution was ambiguous
    

    That can be avoided if you change the iterator_adaptor typedef to:

    typedef boost::iterator_adaptor<
        main_iter_t,
        ForwardIterT,
        const_value_type,
        std::forward_iterator_tag,
        const_value_type
    > type;
    

    This makes both the program below (based on your code) and the example from boost-spirit.com work, but I’m not sure that it won’t break in other cases, so use it at your discretion.

    #include <vector>
    #include <istream>
    #include <sstream>
    #include <iostream>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/support_multi_pass.hpp>
    #include <boost/spirit/include/classic_position_iterator.hpp>
    
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;
    
    int main()
    {
    
        std::string TEST = "aa\nbbbb\nccc\n";
    
        std::istringstream INPUT (TEST);
        std::noskipws(INPUT);
    
        typedef std::istreambuf_iterator<char> base_iterator;
        typedef boost::spirit::multi_pass<base_iterator>  multi_pass_iter;
        typedef boost::spirit::classic::position_iterator2<multi_pass_iter> pos_iterator;
    
        base_iterator base_begin(INPUT);
    
        multi_pass_iter first =  boost::spirit::make_default_multi_pass(base_begin);
        multi_pass_iter last;
    
        pos_iterator pfirst(first,last,std::string("DD"));
        pos_iterator plast;
    
        using qi::lexeme;
        using ascii::alpha;
    
        std::vector<std::string> DDD;
        bool res = qi::phrase_parse(pfirst,plast,* lexeme[+alpha],ascii::space,DDD);
    
        if(res && pfirst==plast)
        {
            for (const auto & d : DDD) 
                std::cout << d << " (" << d.size() << ")" << std::endl;
        }
        else
        {
            std::cout << "Parsing error." << std::endl;
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm trying to parse raw strings into a list (many actually) so let's
I'm trying to get a handle on using boost::spirit to parse character tokens, and
I am trying to parse a list of items which satisfies the python regex
I'm trying to parse this XML I want to get a list of all
I am trying to run a Thread to parse a list of links using
Trying to parse an SQL string and pull out the parameters. Ex: select *
Im trying to parse the string located in /proc/stat in a linux filesystem using
I'm currently trying to consume a list of strings from some XML using Apache
I am trying to parse thru a csv string, put the results into a
I have a list of strings that can contain a letter or a string

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.