开发者

What's the use of the derived class as a template parameter?

开发者 https://www.devze.com 2023-01-21 08:39 出处:网络
What\'s the pur开发者_如何学Pythonpose of this pattern? What is it called? It looked very strange when I saw it the first time, though I have now seen it many times.

What's the pur开发者_如何学Pythonpose of this pattern? What is it called? It looked very strange when I saw it the first time, though I have now seen it many times.

template<typename Derived>
struct Base {
  //...
};

struct Example : Base<Example> {
  //...
};


It's called the Curiously Recurring Template pattern, and allows for static polymorphism.

It's useful when you want to add functionality to a specific class, but want the utility to be usable in a generic case. By making the utility dependent on and use a template parameter, you can achieve both.


I think you are reffering to CRTP. Also refer here


Curiously Recurring Template Pattern, or CRTP as we call it.

0

精彩评论

暂无评论...
验证码 换一张
取 消