c++ - Spirit X3 编译错误 "a template declaration cannot appear at block scope"

标签 c++ boost-spirit-x3

我正在尝试一个小例子来开始使用 Spirit X3。

template <typename Iterator>
bool parser(Iterator& first, Iterator const& last)
{
    using namespace x3;

    x3::rule<class quote,std::string> const quote = "quote";
        auto quote_def = '"' >> *(char_-'"') >> '"';

//   x3::rule<class header,std::vector<std::string>> const header = "header";
//  auto header_def = quote % ',';

    BOOST_SPIRIT_DEFINE(quote);

    return true;
}

int main(int argc, char** argv)
{
    std::string test = "\"abc\",\"def\"";

    auto it=std::begin(test);
    parser( it, std::end(test));

    return 0;
}

我收到以下编译器错误:

In file included from /home/robstr/Downloads/boost_1_60_0/boost/spirit/home/x3/nonterminal.hpp:10:0,
                 from /home/robstr/Downloads/boost_1_60_0/boost/spirit/home/x3.hpp:17,
                 from tmp.cc:5:
tmp.cc: In function ‘bool parser(Iterator&, const Iterator&)’:
/home/robstr/Downloads/boost_1_60_0/boost/spirit/home/x3/nonterminal/rule.hpp:157:5: error: a template declaration cannot appear at block scope
     template <typename Iterator, typename Context, typename Attribute>          \
     ^
/home/robstr/Downloads/boost_1_60_0/boost/preprocessor/seq/for_each.hpp:83:61: note: in expansion of macro ‘BOOST_SPIRIT_DEFINE_’
 # define BOOST_PP_SEQ_FOR_EACH_M_I(r, macro, data, seq, sz) macro(r, data, BOOST_PP_SEQ_HEAD(seq))
                                                             ^
/home/robstr/Downloads/boost_1_60_0/boost/preprocessor/seq/for_each.hpp:78:47: note: in expansion of macro ‘BOOST_PP_SEQ_FOR_EACH_M_I’
 #    define BOOST_PP_SEQ_FOR_EACH_M_IM(r, im) BOOST_PP_SEQ_FOR_EACH_M_I(r, im)
                                               ^
/home/robstr/Downloads/boost_1_60_0/boost/preprocessor/seq/for_each.hpp:77:43: note: in expansion of macro ‘BOOST_PP_SEQ_FOR_EACH_M_IM’
 #    define BOOST_PP_SEQ_FOR_EACH_M(r, x) BOOST_PP_SEQ_FOR_EACH_M_IM(r, BOOST_PP_TUPLE_REM_4 x)
                                           ^
/home/robstr/Downloads/boost_1_60_0/boost/preprocessor/seq/for_each.hpp:77:73: note: in expansion of macro ‘BOOST_PP_TUPLE_REM_4’
 #    define BOOST_PP_SEQ_FOR_EACH_M(r, x) BOOST_PP_SEQ_FOR_EACH_M_IM(r, BOOST_PP_TUPLE_REM_4 x)
                                                                         ^
/home/robstr/Downloads/boost_1_60_0/boost/preprocessor/control/iif.hpp:32:31: note: in expansion of macro ‘BOOST_PP_SEQ_FOR_EACH_M’
 # define BOOST_PP_IIF_1(t, f) t
                               ^
/home/robstr/Downloads/boost_1_60_0/boost/preprocessor/repetition/detail/for.hpp:22:37: note: in expansion of macro ‘BOOST_PP_FOR_1_C’
 # define BOOST_PP_FOR_1(s, p, o, m) BOOST_PP_FOR_1_C(BOOST_PP_BOOL(p(2, s)), s, p, o, m)
                                     ^
/home/robstr/Downloads/boost_1_60_0/boost/preprocessor/cat.hpp:29:34: note: in expansion of macro ‘BOOST_PP_FOR_1’
 #    define BOOST_PP_CAT_I(a, b) a ## b
                                  ^
/home/robstr/Downloads/boost_1_60_0/boost/preprocessor/control/iif.hpp:32:31: note: in expansion of macro ‘BOOST_PP_SEQ_FOR_EACH_DETAIL_CHECK_EXEC’
 # define BOOST_PP_IIF_1(t, f) t
                               ^
/home/robstr/Downloads/boost_1_60_0/boost/preprocessor/seq/for_each.hpp:29:53: note: in expansion of macro ‘BOOST_PP_SEQ_FOR_EACH_DETAIL_CHECK’
 #    define BOOST_PP_SEQ_FOR_EACH(macro, data, seq) BOOST_PP_SEQ_FOR_EACH_DETAIL_CHECK(macro, data, seq)
                                                     ^
/home/robstr/Downloads/boost_1_60_0/boost/spirit/home/x3/nonterminal/rule.hpp:169:34: note: in expansion of macro ‘BOOST_PP_SEQ_FOR_EACH’
 #define BOOST_SPIRIT_DEFINE(...) BOOST_PP_SEQ_FOR_EACH(                         \
                                  ^
src/dico.cc:21:1: note: in expansion of macro ‘BOOST_SPIRIT_DEFINE’
 BOOST_SPIRIT_DEFINE(quote);
 ^

问题是我认为 BOOST_SPIRIT_DEFINE 但我不知道如何解决它。

最佳答案

玩转arround后,我找到了解决办法:

您需要将规则封装在自己的命名空间中:

namespace grammer
{
  using namespace x3;

    x3::rule<class quote,std::string> const quote = "quote";
        auto quote_def = '"' >> *(char_-'"') >> '"';

    x3::rule<class header,std::vector<std::string>> const header = "header";
        auto header_def = quote % ',';


   BOOST_SPIRIT_DEFINE(quote, header);
}


template <typename Iterator>
bool parser(Iterator& first, Iterator const& last)
{
    using namespace x3;
    std::vector<std::string> headerVec;

    auto ret = x3::parse( first, last, grammer::header , headerVec);
    if(ret)
        std::cout << "all done " << headerVec.size() <<"\n";
    else
    {
        std::cout << "not : " << headerVec.size() <<"\n";
    }
    return true;
}

但我无法解释为什么这会解决编译器错误。

关于c++ - Spirit X3 编译错误 "a template declaration cannot appear at block scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35467203/

相关文章:

c++ - PIMPL 和堆栈分配

c++ - DirectX::XMVECTOR 函数 XMVectorSetByIndex() 未设置 float (C++)

c# - Windows 下 C++ 和 C# 中的套接字无法正常工作

c++ - boost.spirit x3 move_to 并列出 ast 成员

c++ - Spirit X3 无匹配函数调用 'move_to'

c++ - 对于可 move 类型,删除 std::vector 中间的元素仍然很昂贵吗?

c++ - 将字符数组初始化为C++中的NULL

c++ - 简单 X3 语法出现难以理解的编译错误

c++ - 使用 bool 属性而不是可选的精神 x3

c++ - 注释期间和之后的 AST 排列