Search code examples
c++c++11s-expressionboost-proto

building s-expression with boost::proto


I'm trying to build s-expression objects using boost::proto with the following terminals:

        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;

and using it like:

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);
}

However this does not compile for me;

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

what i'm doing wrong? any suggestions how to obtain something similar or equivalent to s-expressions using boost::proto ?


Solution

  • I suspect there's something inerently wrong in the way you are using it; I've just started reading this, so I am totally newbie; though, the following compiles and does something

    #include <boost/proto/proto.hpp>
    
    using namespace boost;
    
    typedef proto::terminal< const char* >::type string_term_t;
    
    void testInit()
    {
      auto v = string_term_t ({"foo"});
      proto::display_expr(v);
    }
    
    int main()
    {
      testInit();
      return 0;
    }
    

    In particular I find suspect the usage of the "bare" commas in the definition of v; I don't know if it is expected to be a new feature of C++11 or boost magic, but I won't expect it to work at least as is.

    ADD

    After a little bit of playing, we ended with discovering that the comma operator is an operator iff enclosed inside ( ) and it is not when "bare". The following worked

    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;
    
    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);
    }
    

    (written for the future readers; the () enclosing { "foo" } or whatver can be removed).