gpt4 book ai didi

c++ - boost::spirit 编译问题与 BOOST_FUSION_ADAPT_STRUCT

转载 作者:行者123 更新时间:2023-11-28 08:13:00 27 4
gpt4 key购买 nike

我是 boost::spirit/fusion 的新手。有人可以向我解释为什么以下内容没有编译吗?如果我将 m_namem_settings 变量直接放在配置结构中,编译和工作正常,但是当我将它们分成两个不同的结构时编译失败。我错过了什么?

顺便说一句,使编译器喷出大量 boost::spirit 错误的代码行是:cfg = section >> node;

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

#include <iostream>
#include <string>
#include <vector>

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

struct config_section
{
std::string m_name;
std::string m_settings;
};

struct config
{
config_section m_sections;
};

BOOST_FUSION_ADAPT_STRUCT(
config_section,
(std::string, m_name)
(std::string, m_settings)
)

BOOST_FUSION_ADAPT_STRUCT(
config,
(config_section, m_sections)
)

template <typename Iterator>
struct config_grammar : qi::grammar<Iterator, config(), ascii::space_type>
{
config_grammar() : config_grammar::base_type(cfg)
{
using qi::lexeme;
using qi::lit;
using ascii::string;
using ascii::char_;
using namespace qi::labels;

section %= '[' >> lexeme[+(char_ - ']')] >> ']';

node %= !lit('[') >> lexeme[+(char_ - '\n')];

cfg %= section >> node;
}

qi::rule<Iterator, config(), ascii::space_type> cfg;
qi::rule<Iterator, std::string(), ascii::space_type> node;
qi::rule<Iterator, std::string(), ascii::space_type> section;
};

template <typename Iterator>
bool parse_config(Iterator first, Iterator last)
{
using qi::double_;
using qi::phrase_parse;
using ascii::space;
using boost::phoenix::ref;

config result;
config_grammar<Iterator> config_parser;

bool r = phrase_parse(first, last, config_parser, space, result);
if (first != last) // fail if we did not get a full match
return false;

return r;
}

int main()
{
std::string input = "[section]\nstuff";

bool b = parse_config(input.begin(), input.end());

if (b)
std::cout << "Success" << std::endl;
else
std::cout << "Failure" << std::endl;

return 0;
}

谢谢!

最佳答案

问题似乎是您没有指定如何从 config_section 转换为 config。您告诉 spirit 一个 config_section 由 2 个字符串组成,但尝试在没有规则的情况下解析配置以链接配置和 config_section 之间的转换。这似乎可以编译。

template <typename Iterator>
struct config_grammar : qi::grammar<Iterator, config(), ascii::space_type>
{
config_grammar() : config_grammar::base_type(cfg)
{
using qi::lexeme;
using qi::lit;
using ascii::string;
using ascii::char_;
using namespace qi::labels;

section %= '[' >> lexeme[+(char_ - ']')] >> ']';

node %= !lit('[') >> lexeme[+(char_ - '\n')];

//create a rule to specify conversion of a config_section to a config
cfg %= cfg_sec;
//this a now a cfg_sec (which is what you declared to be composed of 2
// strings)
cfg_sec %= section >> node;
}

qi::rule<Iterator, config(), ascii::space_type> cfg;
//add new declaration here for cfg_sec
qi::rule<Iterator, config_section(), ascii::space_type> cfg_sec;
qi::rule<Iterator, std::string(), ascii::space_type> node;
qi::rule<Iterator, std::string(), ascii::space_type> section;
};

关于c++ - boost::spirit 编译问题与 BOOST_FUSION_ADAPT_STRUCT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8497226/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com