开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜