千家信息网

C++中怎么使用模板提高代码的抽象水平

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇内容主要讲解"C++中怎么使用模板提高代码的抽象水平",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C++中怎么使用模板提高代码的抽象水平"吧!T.1:
千家信息网最后更新 2026年11月03日C++中怎么使用模板提高代码的抽象水平

本篇内容主要讲解"C++中怎么使用模板提高代码的抽象水平",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C++中怎么使用模板提高代码的抽象水平"吧!

T.1:使用模板提高代码的抽象水平

Reason(原因)

Generality. Reuse. Efficiency. Encourages consistent definition of user types.

普遍性。重用。效率。鼓励用户类型的一致性。

Example, bad(反面示例)

Conceptually, the following requirements are wrong because what we want of T is more than just the very low-level concepts of "can be incremented" or "can be added":

概念上,我们希望T不仅限于可以进行增量操作或者可以作为被加数这样非常低水平的概念,因此下面的需求是错误的。

template
// requires Incrementable
T sum1(vector& v, T s)
{
for (auto x : v) s += x;
return s;
}

template
// requires Simple_number
T sum2(vector& v, T s)
{
for (auto x : v) s = s + x;
return s;
}

Assuming that Incrementable does not support+ and Simple_number does not support +=, we have overconstrained implementers of sum1 and sum2. And, in this case, missed an opportunity for a generalization.

假设Incrementable不支持+而且Simple_number不支持+=,我们过分约束了sum1和sum2的实现者。而且,在这种情况下,失去了泛化的机会。

Example(示例)

template
// requires Arithmetic
T sum(vector& v, T s)
{
for (auto x : v) s += x;
return s;
}

Assuming that Arithmetic requires both + and +=, we have constrained the user of sum to provide a complete arithmetic type. That is not a minimal requirement, but it gives the implementer of algorithms much needed freedom and ensures that any Arithmetic type can be used for a wide variety of algorithms.

假设算术运算既需要+也需要+=,我们已经要求sum的用户提供完全的算术类型。这不是最小化的需求,但是它为算法的实现者提供了所需的更多自由,而且保证算术类型可以用于多种多样的算法。

For additional generality and reusability, we could also use a more general Container or Range concept instead of committing to only one container, vector.

为了额外的泛用性和重用性,我们也可以使用更通用的容器或范围概念代替特定的容器vector。

Note(注意)

If we define a template to require exactly the operations required for a single implementation of a single algorithm (e.g., requiring just += rather than also = and +) and only those, we have overconstrained maintainers. We aim to minimize requirements on template arguments, but the absolutely minimal requirements of an implementation is rarely a meaningful concept.

如果我们定义了一个要求用于特定算法的特定实现的操作的模板(例如只要求+=而不同时要求=和+)而且只要求这些,我们就过分约束维护者了。我们的目的在于最小化模板参数的需求,但是某一实现的绝对最小需求几乎不会成为有意义的概念。

Note(注意)

Templates can be used to express essentially everything (they are Turing complete), but the aim of generic programming (as expressed using templates) is to efficiently generalize operations/algorithms over a set of types with similar semantic properties.

模板可以用于从本质上表达任何东西(它们具备图灵完备性),但是泛型编程的目的(像使用模板表达的那样)是高效概括可以适用于具有相似语义属性一套类型的操作/算法。

Note(注意)

The requires in the comments are uses of concepts. "Concepts" are defined in an ISO Technical Specification: concepts. Concepts are supported in GCC 6.1 and later. Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only. If you use GCC 6.1 or later, you can uncomment them.

注释中的需求是concept的用法。"Concepts"被定义在ISO技术规格中。GCC6.1之后的版本都支持Concepts。因此,在例子中我们注释掉相关代码;也就是说,我们只将它们用作标准注释。如果你使用GCC6.1之后的版本,你可以去掉注释符号。

Enforcement(实施建议)

  • Flag algorithms with "overly simple" requirements, such as direct use of specific operators without a concept.

  • 标记需求过于简单的算法,例如不用concept而直接使用特定操作符。

  • Do not flag the definition of the "overly simple" concepts themselves; they may simply be building blocks for more useful concepts.

  • 不要标记定义过于简单的concept本身;它们没准只是作为某个更有用的concept的一部分存在的。

到此,相信大家对"C++中怎么使用模板提高代码的抽象水平"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

模板 需求 代码 水平 算法 概念 注释 类型 C++ 最小 算术 支持 内容 实现者 容器 更多 标记 版本 用户 目的 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网未来科技 浩特互联网科技 润盈互联网科技 新华互联网科技2019 互联网教育科技公司vipkid 互联网校园科技培训 苏州铭阳互联网科技有限公司 链环科技传统企业怎么转型互联网 互联网科技大赛获奖感言 互联网科技公司营业执照怎么办理 烟台灵鸽互联网科技有限公司 工业互联网山东科技大学 互联网时代的科技公司 众安科技工业互联网合作 山东星卡互联网科技有限公司官网 有人说阿里系是互联网科技企业 江苏互联网智能科技产品出厂价 数字互联网科技有限公司 互联网领先科技成果宣传视频 互联网企业和网络科技有限公司 花魁咖啡 北京互联网科技 互联网科技类展会 丰台科技园 互联网公司 成都玖崟互联网科技有限公司乐商圈怎么样 科技与互联网的利与弊 江阴互联网智能科技产品进口 山东有哪些互联网科技公司 沐思想互联网科技有限公司 广州全民伙伴互联网科技有限公司
0