Home > Backend Development > C++ > Can Spirit Parsers Be Used with `auto` Variables?

Can Spirit Parsers Be Used with `auto` Variables?

Patricia Arquette
Release: 2024-12-02 04:03:09
Original
202 people have browsed it

Can Spirit Parsers Be Used with `auto` Variables?

Assigning Parsers to Auto Variables

Question:

Can Spirit parsers be used with auto?

Code Snippet:

auto bracketed_z = '[' >> +qi::char_('z') >> ']';
Copy after login

When assigning a parser to an auto variable, as shown above, the code crashes with a segmentation fault. However, passing the parser directly to qi::parse() inline works fine.

Answer:

No, Spirit parsers are not intended to be used with auto in Spirit V2.

Explanation:

The underlying Proto expression templates hold references to temporaries. Assigning a parser to an auto variable causes the parser to be copied, which may result in dangling references.

Solutions:

To work around this issue, you can use the following methods:

  • qi::copy() (available in the Boost trunk after Boost 1.55.0)
  • boost::proto::deep_copy
  • BOOST_SPIRIT_AUTO

The above is the detailed content of Can Spirit Parsers Be Used with `auto` Variables?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template