2

我正在尝试使用boost::proto以下终端构建 s-expression 对象:

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

但是,这对我来说并不编译;

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 ‘&lt;expression error>’
Test.cpp:18:73: error: expected ‘)’ before ‘(’ token

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

4

3 回答 3

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;
于 2012-04-07T20:51:29.410 回答
1

我怀疑你使用它的方式有一些错误。我刚刚开始阅读这篇文章,所以我完全是新手;但是,以下编译并执行某些操作

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

特别是,我怀疑在 v 的定义中使用“裸”逗号;我不知道它是否有望成为 C++11 的新功能或增强魔法,但我不希望它至少能按原样工作。

添加

玩了一会儿,我们最后发现逗号运算符是一个包含在里面的运算符,( )而它不是“裸”的。以下工作

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

(写给未来的读者;()封闭{ "foo" }或任何可以删除的东西)。

于 2012-04-07T16:30:38.587 回答
0

结果我在不依赖 boost 的情况下做了一个编译时的 S 表达式。唯一的问题是依赖于 c++11


性别公关

#ifndef S_EXPRESSION_H
#define S_EXPRESSION_H

#include <algorithm>

template< typename... Subexprs > class SExpr;

template< typename SExprType, unsigned int Idx > class SExprGetter;

template< typename SExprType >
class SExprGetter< SExprType, 0 >
{
    SExprType* tree;
public:
    typedef typename SExprType::Expr_0_type return_type;

    SExprGetter(SExprType& _v) : tree(& _v) {}
    SExprGetter(SExprGetter&& _o) : tree(_o.tree) {}

    return_type& getValue() { return tree->get_storage(); }

    template< unsigned int Idx>
    SExprGetter< return_type , Idx > getChild()
    {
        return getValue().template getChild<Idx>();
    }

    static constexpr unsigned int childs()
    {
         return return_type::childs();
    }
};

template< typename SExprType >
class SExprGetter< SExprType, 1 >
{
    SExprType* tree;
    typedef typename SExprType::Remainder_type remainder_type_1;
public:
    typedef typename remainder_type_1::Expr_0_type return_type;

    SExprGetter(SExprType& _v) : tree(& _v) {}
    SExprGetter(SExprGetter&& _o) : tree(_o.tree) {}

    return_type& getValue() { return tree->get_remainder_storage().get_storage(); }

    template< unsigned int Idx>
    SExprGetter< return_type , Idx > getChild()
    {
        return getValue().template getChild<Idx>();
    }

    static constexpr unsigned int childs()
    {
         return return_type::childs();
    }
};

template< typename SExprType >
class SExprGetter< SExprType, 2 >
{
    SExprType* tree;
    typedef typename SExprType::Remainder_type remainder_type_1;
    typedef typename remainder_type_1::Remainder_type remainder_type_2;
public:
    typedef typename remainder_type_2::Expr_0_type return_type;

    SExprGetter(SExprType& _v) : tree(& _v) {}
    SExprGetter(SExprGetter&& _o) : tree(_o.tree) {}

    return_type& getValue() { return tree->get_remainder_storage().get_remainder_storage().get_storage(); }

    template< unsigned int Idx>
    SExprGetter< return_type , Idx > getChild()
    {
        return getValue().template getChild<Idx>();
    }

    static constexpr unsigned int childs()
    {
         return return_type::childs();
    }
};

template< typename SExprType >
class SExprGetter< SExprType, 3 >
{
    SExprType* tree;
    typedef typename SExprType::Remainder_type remainder_type_1;
    typedef typename remainder_type_1::Remainder_type remainder_type_2;
    typedef typename remainder_type_2::Remainder_type remainder_type_3;
public:
    typedef typename remainder_type_3::Expr_0_type return_type;

    SExprGetter(SExprType& _v) : tree(& _v) {}
    SExprGetter(SExprGetter&& _o) : tree(_o.tree) {}

    return_type& getValue() { return tree->get_remainder_storage().get_remainder_storage().get_remainder_storage().get_storage(); }

    template< unsigned int Idx>
    SExprGetter< return_type , Idx > getChild()
    {
        return getValue().template getChild<Idx>();
    }

    static constexpr unsigned int childs()
    {
         return return_type::childs();
    }
};


template< typename T0, typename... Subexprs >
class SExpr< T0, Subexprs... >
{
    T0 _t0_storage;
    SExpr< Subexprs... > _remainder_storage;

public:

    typedef T0 Expr_0_type;
    typedef SExpr< Subexprs... > Remainder_type;

    SExpr(T0 _t, Subexprs... sexprs) : _t0_storage(_t), _remainder_storage(sexprs...) {}
    SExpr(T0&& _t, Subexprs && ... sexprs) : _t0_storage(_t), _remainder_storage(sexprs...) {}


    T0& get_storage() { return _t0_storage; }
    Remainder_type& get_remainder_storage() { return _remainder_storage; }

    template< unsigned int Idx>
    SExprGetter< SExpr , Idx > getChild()
    {
        SExprGetter< SExpr, Idx > getter(*this);
        return getter;
    }

    static constexpr unsigned int childs()
    {
         return sizeof...(Subexprs) + 1;
    }
};


template< typename T0>
class SExpr< T0 >
{
    T0 _t0_storage;

    public:

    typedef T0 Expr_0_type;
    typedef void Remainder_type;

    SExpr(T0 _t) : _t0_storage(_t) {}
    SExpr(T0&& _t) : _t0_storage(_t) {}

    T0& get_storage() { return _t0_storage; }

    static constexpr unsigned int childs()
    {
         return 1;
    }

    template< unsigned int Idx>
    SExprGetter< SExpr , Idx > getChild()
    {
        SExprGetter< SExpr, Idx > getter(*this);
        return getter;
    }
};


template <typename... Exprs>
SExpr<Exprs...> _S_expr(Exprs... exprs) {
    return
    {
        exprs...
    };
};

#endif  /* S_EXPRESSION_H */

测试.cpp

#include <iostream>
#include "sexpr.h"
#include <string>

using namespace std;

int main()
{
    auto s_expr_1 = _S_expr(3);
    auto s_expr_2 = _S_expr(3, std::string("foo"));
    auto s_expr_3 = _S_expr(3, _S_expr(3, "bar"), std::string("foo"));

    auto ff_1 = s_expr_1.getChild<0>().getValue();
    cout << " ff_1: " << ff_1 << endl;

    auto ff_2 = s_expr_2.getChild<1>().getValue();
    cout << " ff_2: " << ff_2 << endl;

    auto ff_3 = s_expr_3.getChild<1>().getChild<1>().getValue();
    cout << " ff_3: " << ff_3 << endl;

    cout << " s expr 3 has " << s_expr_3.childs() << " and the first leaf has " << s_expr_3.getChild< s_expr_3.childs() - 2 >().childs() << endl;
};
于 2014-01-04T22:21:12.673 回答