CRTP to avoid dynamic polymorphism
问题 How can I use CRTP in C++ to avoid the overhead of virtual member functions? 回答1: There are two ways. The first one is by specifying the interface statically for the structure of types: template <class Derived> struct base { void foo() { static_cast<Derived *>(this)->foo(); }; }; struct my_type : base<my_type> { void foo(); // required to compile. }; struct your_type : base<your_type> { void foo(); // required to compile. }; The second one is by avoiding the use of the reference-to-base or