하나의 정신 규칙으로 벡터를 작성하고 거기에 값을 추가하고 싶습니다. 가능한가? 나는 아래와 같은 것을 시도했지만 성공하지 못했다. 자세한 내용은 코드 주석을 읽으십시오. 감사.즉시 작성하여 벡터에 작성
typedef std::vector<double> number_array;
typedef std::vector<std::string> string_array;
typedef boost::variant<number_array, string_array> node
template<typename Iterator>
struct parser
: qi::grammar<Iterator, node(), ascii::space_type> {
parser(parser_impl* p)
: parser::base_type(expr_, ""),
error_handler(ErrorHandler(p)) {
// Here I want to create vector on the fly
// and append values to newly created vector.
// but this doesn't compile, specifically phoenix::push_back(...)
number_array_ = qi::lit('n[')[qi::_val = construct<number_array>()] >>
-(qi::double_ % ',')[phoenix::push_back(phoenix::ref(qi::_val), qi::_1)] >> ']';
// this doesn't compile too
string_array_ = qi::lit('s[')[qi::_val = construct<string_array>()] >>
-(quoted_string % ',')[phoenix::push_back(phoenix::ref(qi::_val), qi::_1)] >> ']';
quoted_string %= "'" > qi::lexeme[*(qi::char_ - "'")] > "'";
expr_ = number_array_[qi::_val = qi::_1] | string_array_[[qi::_val = qi::_1]];
}
qi::rule<Iterator, number_array(), ascii::space_type> number_array_;
qi::rule<Iterator, string_array(), ascii::space_type> string_array_;
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
qi::rule<Iterator, node(), ascii::space_type> expr_;
};
예를 참조하십시오. % 연산자를 사용하기 때문에 작동하지 않는 것 같습니다. 문서를 보면 (a % b) 벡터 이므로 벡터에서 벡터를 푸시하려고합니다 (_1은 해당 벡터를 참조 함). – Kiroxas
아니요 _1은 해당 벡터를 참조하지 않습니다. 하지만 그게 당신이 의미하는 것입니다 – sehe