![]() |
Home | Libraries | People | FAQ | More |
Proto provides a utility for pretty-printing expression trees that comes
in very handy when you're trying to debug your EDSL. It's called proto::display_expr()
, and you pass it the expression
to print and optionally, an std::ostream
to which to send the output. Consider:
// Use display_expr() to pretty-print an expression tree proto::display_expr( proto::lit("hello") + 42 );
The above code writes this to std::cout
:
plus( terminal(hello) , terminal(42) )
In order to call proto::display_expr()
,
all the terminals in the expression must be Streamable (that is, they can
be written to a std::ostream
). In addition, the tag types
must all be Streamable as well. Here is an example that includes a custom
terminal type and a custom tag:
// A custom tag type that is Streamable struct MyTag { friend std::ostream &operator<<(std::ostream &s, MyTag) { return s << "MyTag"; } }; // Some other Streamable type struct MyTerminal { friend std::ostream &operator<<(std::ostream &s, MyTerminal) { return s << "MyTerminal"; } }; int main() { // Display an expression tree that contains a custom // tag and a user-defined type in a terminal proto::display_expr( proto::make_expr<MyTag>(MyTerminal()) + 42 ); }
The above code prints the following:
plus( MyTag( terminal(MyTerminal) ) , terminal(42) )