提升精神 如何将本地引用作为属性传递
Boost Spirit how to pass a local reference as attribute
如何将_a作为对具有相同类型属性的子规则的引用传递:
rule(_a)
不起作用。
代码如下所示:
qi::rule<Iterator, Mdlx(), qi::locals<std::string, long32>, Skipper> mdl; // parent rule
qi::rule<Iterator, Model(long32&), Skipper> model; // sub rule
...
mdl =
-version[at_c<0>(_val) = _1]
// wrong code! pass _b because it has the type long32
//>> -model(_a)[at_c<1>(_val) = _1] // pass local as attribute
>> -model(_b)[at_c<1>(_val) = _1] // pass local as attribute
>> -sequences[at_c<2>(_val) = _1]
>> -global_sequences[at_c<3>(_val) = _1]
>> -textures[at_c<4>(_val) = _1]
>> -materials[at_c<5>(_val) = _1]
>> repeat(_a)[
geoset_animation
]
;
model =
lit("Model")
>> string_literal[at_c<0>(_val) = _1]
>> lit('{')
>> -(
lit("NumGeosetAnims")
>> integer_literal[_r1 = _1] // assign the value to the passed attribute
>> lit(',')
)
>> lit("BlendTime") >> integer_literal[at_c<1>(_val) = _1]
>> lit(',')
>> bounds[at_c<2>(_val) = _1]
>> lit('}')
;
如您所见,我想传递一个局部作为对子规则的引用,该子规则应设置 long32 值。然后,该值在父规则中用作局部变量_a。
编辑:我在代码中发现了错误。我正在传递_a但第一个本地具有 std::string 类型。我不得不通过_b!
在澄清问题之后,我推测您正在寻找更像精神继承属性的功能。最重要的是,它们总是在调用时传递,因此它们不需要是默认可构造的,并且可以是引用类型。
住在科里鲁
#define BOOST_SPIRIT_DEBUG
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
template <typename It>
struct my_parser : qi::grammar<It> {
my_parser() : my_parser::base_type(start) {
using namespace qi;
start = my_int_rule;
my_int_rule = my_rule_with_inherited_attribute(_val);
my_rule_with_inherited_attribute = int_ [ _r1 = _1 ]; // copied into the reference passed
BOOST_SPIRIT_DEBUG_NODES((start)(my_rule_with_inherited_attribute)(my_int_rule))
}
private:
qi::rule<It> start;
qi::rule<It, int() > my_int_rule;
qi::rule<It, void(int&) > my_rule_with_inherited_attribute;
};
int main()
{
using It = std::string::const_iterator;
my_parser<It> p;
std::string const input = "123";
bool ok = qi::parse(input.begin(), input.end(), p);
std::cout << "Parse " << (ok? "success":"failed") << "n";
}
指纹
<start>
<try>123</try>
<my_int_rule>
<try>123</try>
<my_rule_with_inherited_attribute>
<try>123</try>
<success></success>
<attributes>[, 123]</attributes>
</my_rule_with_inherited_attribute>
<success></success>
<attributes>[123]</attributes>
</my_int_rule>
<success></success>
<attributes>[]</attributes>
</start>
Parse success
假设我猜对了你在这里的实际意思:
住在科里鲁
#define BOOST_SPIRIT_DEBUG
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
template <typename It>
struct my_parser : qi::grammar<It> {
my_parser() : my_parser::base_type(start) {
using namespace qi;
my_rule_with_a_local = my_int_rule [ _a = _1 ];
my_int_rule = int_;
start = my_rule_with_a_local;
BOOST_SPIRIT_DEBUG_NODES((start)(my_rule_with_a_local)(my_int_rule))
}
private:
qi::rule<It> start;
qi::rule<It, int() > my_int_rule;
qi::rule<It, qi::locals<int> > my_rule_with_a_local;
};
int main()
{
using It = std::string::const_iterator;
my_parser<It> p;
std::string const input = "123";
bool ok = qi::parse(input.begin(), input.end(), p);
std::cout << "Parse " << (ok? "success":"failed") << "n";
}
指纹
<start>
<try>123</try>
<my_rule_with_a_local>
<try>123</try>
<my_int_rule>
<try>123</try>
<success></success>
<attributes>[123]</attributes>
</my_int_rule>
<success></success>
<attributes>[]</attributes><locals>(123)</locals>
</my_rule_with_a_local>
<success></success>
<attributes>[]</attributes>
</start>
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 初始化类C++私有属性(值/引用/静态)
- C++ 如何正确引用类属性的值
- C++对象共享属性 - 使用指针或引用
- 具有仅在某些情况下引用外部变量的属性的类
- Const 引用字段作为类中的只读属性C++
- C++引用基类中的子类指针属性
- 返回泛型函数中对象的引用(具有指针或非指针属性)
- 如何在C++中将结构属性转换为指针引用
- 索引属性如何返回引用
- spirit::qi :将继承的属性引用传递给 phoenix::function
- 提升精神 如何将本地引用作为属性传递
- 使用一个 GL ELEMENT_ARRAY_BUFFER引用 0 中的每个属性
- c++中引用类型的属性
- 如何引用的形式和改变属性(高度和宽度)从一个函数在Qt
- QML 和C++属性 - 引用错误:找不到变量
- 在c++中是否有一个很好的理由在不改变属性的情况下引用它?