使用boost :: proto构建s表达式

lur*_*her 2 c++ s-expression boost-proto c++11

我正在尝试使用boost::proto以下终端构建s表达式对象:

        typedef proto::terminal< const char* >::type string_term_t;
        typedef proto::terminal< uint32_t >::type uint32_term_t;
        typedef proto::terminal< float >::type float_term_t;
Run Code Online (Sandbox Code Playgroud)

并使用它像:

void testInit()
{
    auto v = string_term_t("foo") , string_term_t("bla") , (float_term_t(5.6), string_term_t("some"));
    proto::display_expr(v);
}
Run Code Online (Sandbox Code Playgroud)

但是这不适合我;

Test.cpp:18:33: error: no matching function for call to ‘boost::proto::exprns_::expr<boost::proto::tag::terminal, boost::proto::argsns_::term<const char*>, 0l>::expr(const char [4])’
boost_1_46_0/boost/proto/proto_fwd.hpp:300:16: note: candidates are: boost::proto::exprns_::expr<boost::proto::tag::terminal, boost::proto::argsns_::term<const char*>, 0l>::expr()
boost_1_46_0/boost/proto/proto_fwd.hpp:300:16: note:                 boost::proto::exprns_::expr<boost::proto::tag::terminal, boost::proto::argsns_::term<const char*>, 0l>::expr(const boost::proto::exprns_::expr<boost::proto::tag::terminal, boost::proto::argsns_::term<const char*>, 0l>&)
Test.cpp:18:33: error: unable to deduce ‘auto’ from ‘<expression error>’
Test.cpp:18:73: error: expected ‘)’ before ‘(’ token
Run Code Online (Sandbox Code Playgroud)

我做错了什么?任何建议如何获得类似或等效于s表达式的东西boost::proto

Eri*_*ler 5

proto::expr<>类型没有定义构造函数; 因此,你的问题.尝试定义这样的类型:

typedef proto::literal< const char* > string_term_t;
typedef proto::literal< uint32_t > uint32_term_t;
typedef proto::literal< float > float_term_t;
Run Code Online (Sandbox Code Playgroud)

  • 我还要说创建一个表达式模板并像你正在做的那样分配一个``auto``变量永远不会安全.Proto表达式树通过引用保存内部节点.如果这些节点是临时对象,那么这些节点会在完整表达式(分号)的末尾被删除,从而导致悬空引用.您可以使用`proto :: deep_copy`确保在将表达式树分配给局部变量之前按值存储所有内容.HTH! (6认同)