- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我遇到了很多关于 CRTP 是什么的解释,但没有解释它为什么起作用。
The Microsoft Implementation of CRTP in ATL was independently discovered, also in 1995 by Jan Falkin who accidentally derived a base class from a derived class. Christian Beaumont, first saw Jan's code and initially thought it couldn't possibly compile in the Microsoft compiler available at the time. Following this revelation that it did indeed did work, Christian based the entire ATL and WTL design on this mistake.
例如,
template< typename T >
class Base
{
...
};
class Derived : public Base< Derived >
{
...
};
我了解为什么以及何时可以使用它。但我想知道编译器是如何以这种方式工作的。因为在我看来,由于无休止的递归,它不应该工作:class Derived
继承自 Base< Derived >
, 其中Derived
是继承自 Base< Derived >
的类, 其中Derived
...等等。
能否请您从编译器的角度逐步解释它是如何工作的?
最佳答案
递归定义的类型并不少见:链表也是递归的。它之所以有效,是因为在循环的某个时刻,您不需要类型是完整的,您只需要知道它的名称。
struct LinkedNode {
int data;
LinkedNode *next; // Look ma, no problem
};
对于 CRTP,这一点就在这里:
Base<Derived>
为 Derived
实例化 Base
确实 不 要求 Derived
是完整的,只知道它是一个类类型。即,以下工作正常:
template <class>
struct Foo { };
struct Undefined;
Foo<Undefined> myFoo;
因此,只要 Base
的定义不需要 Derived
是完整的,一切都可以正常工作。
关于c++ - 为什么 Curiously Recurring Template Pattern (CRTP) 有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49708984/
我们正在转向 Recurly 进行计费,并计划使用 recurly.js api 在生产环境中生成计费 token ,但与此同时,在其他环境中进行测试真的很难。理想情况下,我希望能够将信用卡信息从我的
我已经实现了 recurly.js 表单,但在加载 recurly.js 时遇到问题。以下是我的代码:
我正在处理我们公司的购物车,其中包含人们可以订阅或直接购买的产品。问题是它应该要求客户在一个订单中添加这两种类型。 问题:是否可以让 Paypal 将选定的项目作为订阅收费,而其他项目作为一次性付款?
我是 Clojure 的新手,为了练习,我尝试对半随机数应用一个简单的算法。 几天前,我在 clojure 的文档中读到循环,以及它们如何使用 recur 工作,所以我尝试用这行代码编写一个循环: (
我没有对问题的准确描述,所以我只是想问一下这是否可能(如果可能的话,一些其他信息会很好)。 一位程序员告诉我,您可以避免由虚函数/多态性引起的运行时开销。他说,为了避免运行时开销,您可以在名为 Cur
我在本地主机 Windows 10 上运行 xampp Apache/2.4.7 PHP 5.5.9 当我尝试调用 Recurly PHP API(API 版本 = 2.7.0)时,它返回此错误 Co
我有一个关于迭代和与迭代类似的 Clojure 库函数的问题。 (defn iterate 2 "Returns a lazy sequence of x, (f x), (f (f x))
(defn matrix-diagonals-odd-p ([matrix] (matrix-diagonals-odd-p matrix 0)) ([matrix offset]
本文整理了Java中jgnash.engine.recurring.YearlyReminder类的一些代码示例,展示了YearlyReminder类的具体用法。这些代码示例主要来源于Github/S
我正在为 this 写一个答案挑战,当我需要给递归函数一个可选参数时。我最终得到了一些相当于: (defn func [a & [b?]] (if b? b? (recur a a
在以下(Clojure)SO问题中:my own interpose function as an exercise 接受的答案是这样的: Replace your recursive call wi
我正在编写一个小型应用程序来为人们处理任务。非常简单,但就表格设计而言,我所坚持的领域是重复性任务的情况,可以是一次、每天、每周或每月。如果每周,则为每周的特定日期。每月是特定的一天。 我有一个任务表
据我了解,在 Clojure 中递归而不使用循环 .. recur 语法对于短序列来说可能不是问题。但是,使用loop .. recur 语法是编写递归函数的首选方法。因此,我想从首选方法开始。 但是
我正在使用基于 json 的递归 api 进行订阅调用,但看起来卡总是按计划值收费。我试图通过在订阅对象中发送 unit_amount_in_cents 来更改计划的单位值,但它不起作用。 以下是通话
我正在开发一个应用程序,需要允许网站管理员安排“事件”并允许访问者预订事件的位置。 为了最大限度地减少管理工作,我需要允许管理员创建一个事件并将其标记为按计划重复发生。在大多数情况下,这将是“每周”,
我正在搜索 Recurlyjs 正常 Paypal 结帐流程的实现示例。除了 Paypal token 之外,我想向用户请求账单信息,但不清楚如何将它们合并在一起使用。 以下是提及正常工作流程但没有详
++++++++++++++ 实际情况:我在一个站点(使用 C# 的 ASP.Net)上工作,系统将有 3 种不同的订阅计划,即每月、每季度和每年。所有订阅计划都有自己的成本和定价。现在,如果系统/管
我已经在 php 中实现了 paypal 定期订阅计划,并且 paypal 经常性还根据重复周期通过 paypal 收费。但是我的系统没有发现下一次经常性付款是否已收费,所以我怎么知道下一次经常性付款
我一直在尝试实现 chainl1 的尾递归版本,但即使使用循环递归它也会引发 StackOverflowError。这怎么可能,我能做些什么来改变它? (defn atest [state] (w
我读过其他人关于在 Clojure 中遇到堆栈溢出问题的问题,问题往往是在某处建立了一个懒惰的序列。这似乎是这里的问题,但对于我的生活,我不知道在哪里。 这是代码,代码之后是一些解释: (defn p
我是一名优秀的程序员,十分优秀!