gpt4 book ai didi

c++ - 具有相同简单改编结构属性的 boost::spirit::qi 规则会产生编译错误

转载 作者:太空狗 更新时间:2023-10-29 21:45:12 24 4
gpt4 key购买 nike

我想重用一个规则,只需在它前面加上一个关键字。所以我的规则是这样的:

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>

#include <string>

namespace qi = boost::spirit::qi;

struct A {
int index;
std::string name;
};
BOOST_FUSION_ADAPT_STRUCT(::A, (int, index)(std::string, name))

struct B {
A data;
};
BOOST_FUSION_ADAPT_STRUCT(::B, (A, data))

int main() {
typedef std::string::const_iterator iterator_type;

qi::rule<iterator_type, A()> a_rule = qi::int_ > qi::lit(",") > *(qi::char_);
qi::rule<iterator_type, B()> b_rule = qi::lit("(") > a_rule > qi::lit(")");
qi::rule<iterator_type, B()> bad_rule = qi::lit("keyword") > b_rule;

return 0;
}

这不会编译,因为编译器想要从 B 创建 A(引用 bad_rule 的定义):

C:/tc/gcc_x64_4.8.1_win32_seh_rev1/mingw64/my/src/boost_1_54_0/boost/spirit/home/qi/detail/assign_to.hpp:152:18: error: no matching function for call to 'A::A(const B&)'
attr = static_cast<Attribute>(val);
^

然而,将 struct B 更改为不那么简单,使其工作:

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>

#include <string>

namespace qi = boost::spirit::qi;

struct A {
int index;
std::string name;
};
BOOST_FUSION_ADAPT_STRUCT(::A, (int, index)(std::string, name))

struct B {
A data;
int dummy;
};
BOOST_FUSION_ADAPT_STRUCT(::B, (A, data)(int, dummy))

int main() {
typedef std::string::const_iterator iterator_type;

qi::rule<iterator_type, A()> a_rule = qi::int_ > qi::lit(",") > *(qi::char_);
qi::rule<iterator_type, B()> b_rule = qi::lit("(") > a_rule > qi::lit(")") > qi::attr(0);
qi::rule<iterator_type, B()> bad_rule = qi::lit("keyword") > b_rule;

return 0;
}

任何想法,如何在没有这种解决方法的情况下相处以及这里发生了什么?

最佳答案

这又是一个适应单元素序列的问题。不幸的是,我不知道这种情况的解决方法。在这种情况下,您可以删除 B 的改编并专门化 transform_attribute .有一个简单的例子here这几乎完全符合您的要求。

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>

#include <string>

namespace qi = boost::spirit::qi;

struct A {
int index;
std::string name;
};
BOOST_FUSION_ADAPT_STRUCT(::A, (int, index)(std::string, name))

struct B {
A data;
};

namespace boost{ namespace spirit{ namespace traits
{
template <>
struct transform_attribute<B,A,qi::domain>
{
typedef A& type;

static type pre(B& val){ return val.data;}
static void post(B&, A const&){}
static void fail(B&){}
};
}}}

int main() {
typedef std::string::const_iterator iterator_type;

qi::rule<iterator_type, A()> a_rule = qi::int_ >> qi::lit(",") >> *(qi::char_-qi::lit(")"));
qi::rule<iterator_type, B()> b_rule = qi::lit("(") >> a_rule >> qi::lit(")");
qi::rule<iterator_type, B()> bad_rule = qi::lit("keyword") >> b_rule;

std::string test="keyword(1,one)";
iterator_type iter=test.begin(), end=test.end();

B b;

bool result = qi::parse(iter,end,bad_rule,b);
if(result && iter==end)
{
std::cout << "Success:" << std::endl;
std::cout << "b.data.index=" << b.data.index << ", b.data.name=" << b.data.name << std::endl;
}
else
{
std::cout << "Failed. Unparsed: " << std::string(iter,end) << std::endl;
}

return 0;
}

关于c++ - 具有相同简单改编结构属性的 boost::spirit::qi 规则会产生编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18167412/

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