template<class Derived>
class AST::BaseListener< Derived >
CRTP base class for AST listeners. CRTP is a kind of language hack that makes static polymorphism possible in C++.
Instead of using runtime virtual dispatch and RTTI (which incur performance penalties), the base class is declared as a template, and the derived class is instantiated as class Derived : public Base<Derived> (i.e., passes itself as the template parameter)
Normally, a base class could never call a derived class's functions directly, because those derived functions could not possibly have been declared at the time the base class was defined (this results in a compiler error, the compiler says "what function's that then? I haven't heard of it yet").
But doing things this way means that the base class is not as yet "defined" until the derived class is defined (since the derived class is needed to instantiate the base class template).
The result is that the base class can now call functions of the derived class directly, STATICALLY, at compile-time.
This allows us to avoid the overhead of virtual function calls and RTTI lookups, while still allowing the derived class to implement only the functions it cares about.
If this is a bit confusing, that's fine. Some people call CRTP an "advanced C++ technique" – this is code for TOTAL HACK. Hacky tricks like this are bound to be confusing. The important thing is that it works.
- Template Parameters
-
| Derived | The derived listener class. |