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

发布时间:2021-11-24 11:31:19 作者:iii
来源:亿速云 阅读:120

本篇内容主要讲解“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<typename T>
   // requires Incrementable<T>
T sum1(vector<T>& v, T s)
{
   for (auto x : v) s += x;
   return s;
}

template<typename T>
   // requires Simple_number<T>
T sum2(vector<T>& 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<typename T>
   // requires Arithmetic<T>
T sum(vector<T>& 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(实施建议)

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

推荐阅读:
  1. c++中模板的实现(模板类和模板函数)
  2. C++中的模板template小结

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:C++怎么使用span<T>或者span_p<T>

下一篇:docker中容器架构、镜像分层特性、dockerfile缓特性的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》