boost.proto + detect invalid terminal before building the expression tree

ⅰ亾dé卋堺 提交于 2019-12-05 12:11:19

Very nice. Now you need to define a grammar that only accepts valid vector expressions something like this:

struct vector_grammar_untyped
  : proto::or_<
        scalar_terminal,
        vector_terminal,
        proto::nary_expr<proto::_, proto::vararg<vector_grammar_untyped> >
    >
{};

struct vector_grammar
  : proto::and_<
        vector_grammar_untyped,
        proto::if_<mpl::not_equal_to< mpl::int_<-1>, vec_dim_check >()>
    >
{};

Then, you change your definition of vector_domain as follows:

struct vector_domain
    : proto::domain <proto::generator <vector_expr>, vector_grammar >
{};

That should keep you from creating expressions that don't pass your custom type-checking. The second template parameter to proto::domain is the grammar to which all expressions in that domain must conform.

Disclaimer: The above code is untested, but it should get you moving in the right direction.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!