C++核心准则T.2:使用模板表现可以适用于多种参数类型的算法

it2023-03-01  77

T.2: Use templates to express algorithms that apply to many argument types

T.2:使用模板表现可以适用于多种参数类型的算法

 

Reason(原因)

Generality. Minimizing the amount of source code. Interoperability. Reuse.

泛用性。最小化代码量。互用性。重用。

 

Example(示例)

That's the foundation of the STL. A single find algorithm easily works with any kind of input range:

这是STL(标准模板库)的基础。一个单纯的检索算法可以简单地适用于任何类型的输入范围:

template<typename Iter, typename Val> // requires Input_iterator<Iter> // && Equality_comparable<Value_type<Iter>, Val> Iter find(Iter b, Iter e, Val v) { // ... }

Note(注意)

Don't use a template unless you have a realistic need for more than one template argument type. Don't overabstract.

如果你没有实际的需求需要多个参数类型,不要使用模板。不要过度抽象。

 

Enforcement(实施建议)

??? tough, probably needs a human

很难。可能需要认为判断。

 

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t2-use-templates-to-express-algorithms-that-apply-to-many-argument-types

 

新书介绍

《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。

 


 

觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

 

 

 

最新回复(0)