首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >简单的Spirit X3单词拆分器无法编译,属性不匹配

简单的Spirit X3单词拆分器无法编译,属性不匹配
EN

Stack Overflow用户
提问于 2018-08-29 21:46:12
回答 1查看 138关注 0票数 1

我正在尝试使用Spirit X3解析器来处理来自命令行工具的输出,但是遇到了问题。我已经将它们缩小到一个我不理解其行为的最小示例:

代码语言:javascript
复制
#include <string>
#include <vector>
#include <boost/spirit/home/x3.hpp>

int main() {
    namespace x3 = boost::spirit::x3;

    std::wstring const str = L"bonjour je suis un  petit panda";

    auto word = x3::lexeme[+x3::alpha];

    std::wstring s;
    x3::phrase_parse(begin(str), end(str), word, x3::space, s); // OK

    std::vector<std::wstring> v;
    x3::phrase_parse(begin(str), end(str), *word, x3::space, v); // Compiler error
}

Live demo on Coliru

这个错误非常复杂,但归根结底是无法调用move_to,这是属性类型不匹配的症状。

代码语言:javascript
复制
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:180:9: error: no matching function for call to 'move_to'
        detail::move_to(std::move(src), dest
        ^~~~~~~~~~~~~~~

[...]

/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:56:9: note: candidate function not viable: no known conversion from 'typename attribute_category<basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > >::type' (aka 'boost::spirit::x3::traits::container_attribute') to 'boost::spirit::x3::traits::unused_attribute' for 3rd argument
        move_to(Source&&, Dest&, unused_attribute) {}
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:74:9: note: candidate function not viable: no known conversion from 'typename attribute_category<basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > >::type' (aka 'boost::spirit::x3::traits::container_attribute') to 'boost::spirit::x3::traits::plain_attribute' for 3rd argument
        move_to(Source&& src, Dest& dest, plain_attribute)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:97:9: note: candidate function not viable: no known conversion from 'typename attribute_category<basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > >::type' (aka 'boost::spirit::x3::traits::container_attribute') to 'boost::spirit::x3::traits::tuple_attribute' for 3rd argument
        move_to(Source&& src, Dest& dest, tuple_attribute)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:106:9: note: candidate function not viable: no known conversion from 'typename attribute_category<basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > >::type' (aka 'boost::spirit::x3::traits::container_attribute') to 'boost::spirit::x3::traits::tuple_attribute' for 3rd argument
        move_to(Source&& src, Dest& dest, tuple_attribute)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:150:9: note: candidate function not viable: no known conversion from 'typename attribute_category<basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > >::type' (aka 'boost::spirit::x3::traits::container_attribute') to 'boost::spirit::x3::traits::variant_attribute' for 3rd argument
        move_to(Source&& src, Dest& dest, variant_attribute tag)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:85:35: note: candidate template ignored: disabled by 'enable_if' [with Source = const wchar_t, Dest = std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >]
        inline typename enable_if<is_container<Source>>::type
                                  ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:113:9: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        move_to(Source&& src, Dest& dest, variant_attribute, mpl::false_)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:143:9: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        move_to(Source&& src, Dest& dest, variant_attribute, mpl::true_)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:157:9: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        move_to(Iterator, Iterator, unused_type, unused_attribute) {}
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:161:9: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        move_to(Iterator first, Iterator last, Dest& dest, container_attribute)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:171:9: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        move_to(Iterator first, Iterator last, boost::iterator_range<Iterator>& rng, container_attribute)
        ^

我的目标是用空格将句子分词。word解析器如预期的那样将第一个完整的单词返回到std::string中。为什么*word不能直接与std::vector<std::string>兼容,我应该写什么呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-30 03:38:06

即使对于第一种情况,我也不确定它是否真的有效。

您正在使用ASCII版本的字符解析器(x3::alphax3::standard::alpha的同义词),传递的迭代器值类型为wchar_t,但对于非ascii字符,boost::spirit::char_encoding::standard::ischar()将返回false。

对于x3::standard_wide::alpha,它是有效的:

代码语言:javascript
复制
#include <string>
#include <vector>
#include <boost/spirit/home/x3.hpp>

int main() {
    namespace x3 = boost::spirit::x3;

    std::wstring const str = L"bonjour je suis un  petit panda";

    auto word = x3::lexeme[+x3::standard_wide::alpha];

    std::wstring s;
    x3::phrase_parse(begin(str), end(str), word, x3::space, s); // OK

    std::vector<std::wstring> v;
    x3::phrase_parse(begin(str), end(str), *word, x3::space, v); // OK
}

另一个很好的问题是它是否适用于ASCII,以及x3::standard::spacex3::standard_wide::space之间是否存在差异(由于Unicode,人们可能会将更多的字符视为空格)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52078975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档