
The definition of SamplingState became more concise, but at the expense of the one of fsm_state that swelled! Indeed, here is how it was before: template Typename Sampler, typename Led, typename Sender>
How to look at suitecrm templates code#
To make this code compile we need to adapt the definition of fsm_state to accept template template parameters: template typename fsm, This is indeed shorter and has less angle brackets. To remove the duplication of the template parameters packs, we can pass template template parameters instead of template type parameters: template Let’s go back to our initial SampleState class: template Let’s now use this C++ feature to simplify our initial template code. We can then instantiate this type with a template parameter: MyTemplateTemplateClass y This means that the parameter we pass is itself a template: template typename Templ> Another parameter we can pass is a template (as opposed to a type). The type can then be instantiated with a type parameter: MyTemplateClass x īut we can put quite a few other other things than types in template parameters. They are defined with typename (or class): template In most of the examples we see in C++ programming courses, and in most template code out there (that I’ve seen), the parameters used in template declarations are types. How would you go about making the definition of SamplingState more expressive?Ī natural solution for this in C++ is to use template template parameters. The problem of SamplingState is that it has a long declaration, because of repeated template parameters: Sampler, Led, Sender. Our goal is now to simplify the code of SampligState. The derived class therefore has to pass itself entirely as a template parameter of the base class, and this implies passing its own template parameters along with itself. But in this case the derived class happens to be a template as well. This is not a requirement of the CRTP, as the CRTP only requires a base template class and the derived class can be anything. In this usage of the CRTP, the derived class is itself a template class. Here is the first one: templateĪ third class uses the two above classes: templateĬlass SamplingState : public fsm_state, SamplingState>įsm_state is used as a CRTP base class on its second parameter: SamplingState passes itself as a template parameter of its base class. It uses two independent template classes.
How to look at suitecrm templates how to#
We’re going to examine this case where template parameters became unwieldy, and see how to simplify them by using template template parameters. This is an interesting question and I’m grateful to Sam for bringing it up. Fluent C++ reader Sam wrote to me asking how to make a template expression simpler.
