Boost.YAP has a convenient print()
function, that prints an expression tree to a stream. It is not intended
for production work (for instance, it has no formatting options), but it
is excellent for debugging and instrumentation.
Since it is only a debugging aid, print()
is found in a separate header not included when you include Boost.YAP with
#include <boost/yap/yap.hpp>
You must include <boost/yap/print.hpp>
explicitly.
print()
handles several patterns
of expression specially, to allow a concise representation of a given expression
tree. For example, given this definition:
struct thing {};
and this expression:
using namespace boost::yap::literals; auto const const_lvalue_terminal_containing_rvalue = boost::yap::make_terminal("lvalue terminal"); double const d = 1.0; auto rvalue_terminal_containing_lvalue = boost::yap::make_terminal(d); auto thing_terminal = boost::yap::make_terminal(thing{}); auto expr = 4_p + std::move(rvalue_terminal_containing_lvalue) * thing_terminal - const_lvalue_terminal_containing_rvalue;
print()
produces this output:
expr<-> expr<+> term<boost::yap::placeholder<4ll>>[=4] expr<*> term<double &>[=1] term<thing>[=<<unprintable-value>>] & term<char const*>[=lvalue terminal] const &
As you can see, print()
shows one node per line,
and represents the tree structure with indentation. It abbreviates non-terminal
nodes in the tree expr<op>
,
where op
is an operator symbol.
Terminal nodes are abbreviated term<T>
,
where T
is the type of value
contained in the terminal; this may be a reference type or a value.
A term
node may not be a
terminal node at all, but an expr_kind::expr_ref
expression containing a
terminal. Such a expr_kind::expr_ref
node has a &
or const
&
suffix, to indicate that it
is a mutable or const
reference,
respectively.
Each term
node has a bracketed
value near the end. The format is [=X]
where
X
is the value the terminal
contains. If the terminal contains a value for which no operator<<(std::ostream &, ...)
overload exists (such as the thing
type above), X
will be <<unprintable-value>>
.