"奇怪的反复出现的模板图案 "的实际用途[英] Practical Uses for the "Curiously Recurring Template Pattern"

本文是小编为大家收集整理的关于"奇怪的反复出现的模板图案 "的实际用途的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

" 好奇地重复浏览模板模式"? " 计数类别"常见的示例"不可令人信服/p>

推荐答案

模拟动态绑定. 避免在保留某些层次结构优势的同时,避免使用虚拟功能调用的成本,这对于子系统可以在我目前正在从事的项目中完成的子系统赢得了巨大的胜利.

其他推荐答案

它对于 Mixins (我的意思是您从中继承以提供功能)特别有用,这些功能本身需要知道它们正在运行的类型(因此需要是模板).

在有效的C ++ 中,Scott Meyers作为示例提供了类模板NewHandlerSupport .这包含一种静态方法来覆盖特定类的新处理程序(就像std :: set_new_handler为默认运算符新手使用的方式一样),以及使用处理程序的新运营商.为了提供每个类型处理程序,父类需要知道其作用于哪种类型,因此需要是类模板.模板参数是子类.

您没有CRTP真的无法做到这一点,因为您需要单独实例化的newhandlersupport模板,并使用单独的静态数据成员来存储当前的new_handler,每个类都使用它.

显然,整个示例非常非线程安全,但说明了这一点.

Meyers建议CRTP可能被认为是"为我做的".我会说任何混合物通常都是这种情况,并且在您需要Mixin模板而不是仅仅是Mixin类的情况下,CRTP适用.

其他推荐答案

如果您认为仅在方法扩展时需要传递给超类的子类型,则CRTP会变得不那么好奇. 因此,所有类型均被定义. 您只需要将符号子类类型导入超级类的模式,但它只是一个正向声明 - 因为所有正式的模板param类型都是定义的 - 就超级类而言.

.

我们以某种修改的形式使用,将特征类型结构中的子类传递给超级类,以使超级类返回派生类型的对象.该应用程序是用于几何微积分(点,向量,行,框)的库,其中所有通用功能均在超级类中实现,并且子类仅定义了特定类型:cfltpoint从tgenpoint继承.同样在tgenpoint之前也存在cfltpoint,因此子类是一种自然的重构方式.

本文地址:https://www.itbaoku.cn/post/627658.html

问题描述

What are some practical uses for the "Curiously Recurring Template Pattern"? The "counted class" example commonly shown just isn't a convincing example to me.

推荐答案

Simulated dynamic binding. Avoiding the cost of virtual function calls while retaining some of the hierarchical benefits is an enormous win for the subsystems where it can be done in the project I am currently working on.

其他推荐答案

It's also especially useful for mixins (by which I mean classes you inherit from to provide functionality) which themselves need to know what type they are operating on (and hence need to be templates).

In Effective C++, Scott Meyers provides as an example a class template NewHandlerSupport<T>. This contains a static method to override the new handler for a particular class (in the same way that std::set_new_handler does for the default operator new), and an operator new which uses the handler. In order to provide a per-type handler, the parent class needs to know what type it is acting on, so it needs to be a class template. The template parameter is the child class.

You couldn't really do this without CRTP, since you need the NewHandlerSupport template to be instantiated separately, with a separate static data member to store the current new_handler, per class that uses it.

Obviously the whole example is extremely non-thread-safe, but it illustrates the point.

Meyers suggests that CRTP might be thought of as "Do It For Me". I'd say this is generally the case for any mixin, and CRTP applies in the case where you need a mixin template rather than just a mixin class.

其他推荐答案

The CRTP gets a lot less curious if you consider that the subclass type that is passed to the superclass is only needed at time of method expansion. So then all types are defined. You just need the pattern to import the symbolic subclass type into the superclass, but it is just a forward declaration - as all formal template param types are by definition - as far as the superclass is concerned.

We use in a somewhat modified form, passing the subclass in a traits type structure to the superclass to make it possible for the superclass to return objects of the derived type. The application is a library for geometric calculus ( points, vectors, lines, boxes ) where all the generic functionality is implemented in the superclass, and the subclass just defines a specific type : CFltPoint inherits from TGenPoint. Also CFltPoint existed before TGenPoint, so subclassing was a natural way of refactoring this.