C++核心准则T.60:最小化模板对上下文的依赖

网友投稿 677 2022-10-13

C++核心准则T.60:最小化模板对上下文的依赖

C++核心准则T.60:最小化模板对上下文的依赖

T.60: Minimize a template's context dependencies

T.60:最小化模板对上下文的依赖

Reason(原因)

Eases understanding. Minimizes errors from unexpected dependencies. Eases tool creation.

让理解更容易。尽量减少由于意外的依赖引发的错误。方便工具生成。

Example(示例)

templatevoid sort(C& c){ std::sort(begin(c), end(c)); // necessary and useful dependency}templateIter algo(Iter first, Iter last){ for (; first != last; ++first) { auto x = sqrt(*first); // potentially surprising dependency: which sqrt()? helper(first, x); // potentially surprising dependency: // helper is chosen based on first and x TT var = 7; // potentially surprising dependency: which TT? }}

Note(注意)

Templates typically appear in header files so their context dependencies are more vulnerable to #include order dependencies than functions in .cpp files.

模板通常出现在头文件中,相比在CPP文件中的情况,它们的上下文依赖会更容易受到#include次序的影响。

Note(注意)

Having a template operate only on its arguments would be one way of reducing the number of dependencies to a minimum, but that would generally be unmanageable. For example, algorithms usually use other algorithms and invoke operations that do not exclusively operate on arguments. And don't get us started on macros!

让模板操作仅仅操作参数是一种将依赖个数减少到最小的一种方式。但是那样通常会无法实现。例如,算法通常会使用其他算法并调用无法对参数进行排他操作的操作。

See also: T.69

参见:T.69

Enforcement(实施建议)

- Tricky

不好办

原文链接

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

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

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

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Utf8Json - 用于C#超快的JSON序列化程序
下一篇:SMPHP框架一个简单可信赖的php框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~