前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则T.2:使用模板表现可以适用于多种参数类型的算法

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

作者头像
面向对象思考
发布2020-08-27 15:50:56
6390
发布2020-08-27 15:50:56
举报

月季

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 月季
  • T.2: Use templates to express algorithms that apply to many argument types
  • Reason(原因)
    • Note(注意)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档