The expr
class derive from ast
which has a bool() operator that can be used for this purpose. This means we can simply write
if (exp)
cout << exp;
(Internally expressions and ASTs are simply pointers, and they should be valid whenever the pointer is non-zero, so the bool() operator simply checks for nonzero pointers: return m_ast != 0;
).