- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
考虑一个 Logger
类,它有一个为标准 C++ 类型重载的成员函数 write()
,还有一些方便的函数模板,比如 writeLine()
内部调用 write()
:
class Logger {
public:
void write(int x) { ... }
void write(double x) { ... }
...
template <typename T>
void writeLine(T x) { write(x); ... }
...
};
进一步考虑一个子类 FooLogger
,它为特定于域的类型添加了额外的 write()
重载(我们称其中两个为 FooType1
和 FooType2
):
class FooLogger : public Logger {
public:
using Logger::write;
void write(FooType1 x) { ... }
void write(FooType2 x) { ... }
...
};
( self-contained example program at Ideone )
FooLogger::write()
,当直接调用时,现在支持两个类中任一个提供重载的任何参数。
但是,FooLogger::writeLine()
仅支持类 Logger
具有 write()
重载的参数类型...它没有看到在类 FooLogger
中声明的附加 write()
重载。
不过,我希望它能看到它们,这样它也可以用这些参数类型来调用!
我使用 Curiously Recurring Template Pattern (CRTP) 让它工作:
template <typename TDerivedClass>
class AbstractLogger {
...
template <typename T>
void writeLine(T x) { static_cast<TDerivedClass*>(this)->write(x); ... }
};
class Logger : AbstractLogger {}
class FooLogger : public AbstractLogger<FooLogger> {
...
};
( self-contained example program at Ideone )
虽然它完成了工作,但它是以增加代码复杂性和冗长为代价的:
AbstractLogger
和Logger
分成两个类。.cpp
文件)——即使是那些没有的需要做 static_cast
事情。考虑到上述情况,我正在寻求具有 C++ 经验的人的见解:
最佳答案
换个方式怎么样:
template <typename ...Ts>
class Logger : private Ts...
{
public:
using Ts::write...;
void write(int x) { /*...*/ }
void write(double x) { /*...*/ }
// ...
template <typename T>
void writeLine(T x) { write(x); /*...*/ }
// ...
};
class FooWriter
{
public:
void write(FooType1 x) { /*...*/ }
void write(FooType2 x) { /*...*/ }
};
using FooLogger = Logger<FooWriter>;
然后使用任何(或它们的别名):
Logger<>
或 Logger<FooWriter>
或 Logger<FooWriter, BarWriter>
...
关于c++ - Curiously Recurring Template Pattern (CRTP) 是正确的解决方案吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54119501/
我们正在转向 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
我是一名优秀的程序员,十分优秀!