This blog post describes two usages of CRTP
- 1) template method
- 2) static polymorphism to avoid penalty of virtual functions
- —Other usages of CRTP not described here:
- virtual clone (similar to TemplateMethod?)– https://github.com/tiger40490/repo1/blob/cpp1/cpp/template/CRTP_clone.cpp
- count frequency of ctor call for each concretized class. No static_cast to Sub*
I briefly read the excellent blog https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/. I feel CRTP is advanced for almost all the contexts I can think of. (In contrast, I can see some “necessary” usages of SFINAE, such as the one in my little AddOrder.h)
https://stackoverflow.com/questions/262254/crtp-to-avoid-dynamic-polymorphism shows a simple  example of CRTP to replace virtual functions. How much efficiency improvement does it make? Questionable. I always say that if you want the lowest latency, then write selected modules in assembly language, and store it in hardware like FPGA.
 if there is anything simple in template meta-programming.
I have heard of several techniques to avoid virtual functions, but I believe the actual evidence (in terms of measured improvement in latency) is likely unconvincing or insignificant. Therefore, if CRTP is used to eliminate virtual function latency, then I am not sure how much value it adds.
There are other techniques to avoid “virtual”. I feel they are easier to understand than CRTP.
Sometimes CRTP is the only choice — if the virtual function needs its own template parameters, then compiler will complain that “function template can’t be virtual”. Rahul hit this complaint.
Q: for a true virtual method v1(), the derived class is not yet written when the base class is compiled. Later, Only at run time can the “system” pick the right implementation of v1(). How about CRTP?
A: base class is Not compiled ahead of the derived class. Each derived class includes a header defining the base class template.
Beside this “virtual-elimination” use case, CRTP has other applications (am still unfamiliar with), but if I’m asked in interviews I will only mention this one use case. One of the common “other usages” is TemplateMethod with compile time (not runtime) resolution, the 1st usage in the excellent blog . In the classic template method pattern, the “procedure” is published and finalized in the base class. Individual steps of the procedure are virtual methods, resolved at runtime. In the CRTP version, superclass methods call subclass methods, safely and cleanly.
Superclass using subclass is a taboo in most contexts, but both traditional TemplateMethod and CRTP-TemplateMethod are notable exceptions.
The article didn’t clearly highlight a key point about this usage — The base class NumericalFunctions is general purpose, designed to be subclassed by anyone. I could write a Temperature class to subclass NumericalFunctions too. This way, the code in NumericalFunctions is available for reuse.
https://github.com/tiger40490/repo1/blob/cpp1/cpp/template/CRTP_demo1.cpp is working code. Key points to remember about the code sample:
- base-class — is a template with a dummy type “Sub”
- derived classes — have the form “class Sub1 public Base<Sub1>”
- the static dispatch (non-virtual) function in Base always static_cast “this” to *Sub.