《C++ Templates第二版》个人翻译(一)

仅供自己学习,翻译不一定准确

第一部分 基本概念

第一部分介绍的是C++模板的基本概念和语言特性。该部分首先通过展示函数模板(function templates)和类模板(class templates)来详述C++模板的目标与相关概念。接下来则是讨论一些基本模板,如非类型模板参数(nontype template parameters),可变参数模板(variadic templates),关键字“typename"和成员模板(menber templates)。

同时该部分还讲诉了如何处理移动语义(move semantics),如何声明参数与如何使用泛型代码(generic code)来进行编译时编程(compile-time programming)。最后,该部分还包括了对于相关术语使用的提示,以及作为一位应用程序员或通用库(generic libraries)作者在实际中对C++模板的实践和应用。

为什么要用模板 

在C++中,我们去声明变量,函数,和大多数使用特定类型的实体(entities)。但是对于不同类型来说,它们的代码常常看起来十分相似。

举个例子,存放int类型的数组和存放着字符串类型的向量(vectors),只要它们所包含的类型之间能够相互比较出大小,那么它们的快速排序(quicksort)算法的实现从结构上来看就是一模一样的。

如果你使用的编程语言不支持这种泛型的特殊语言特性,那你只能采取以下这几种方法:

1.你可以对那些需要此行为的每个类型实现相同的行为(behavior)。

2.你可以为通用基类型(如实体类(object)和无类型指针(void*))编写通用的代码。

3.你可以使用特殊的预处理器。

如果你以前学过其他编程语言,你可能已经尝试过上面列举的某种或三种方法。然而,它们各自有着缺点:

1.如果你重复为每个类型实现相同的行为,那这就是在浪费时间(reinvent the wheel)。你会在编程中不停碰到相同的错误,而且你会为了不碰见更多的错误而去避免使用那些虽然复杂但是更好的算法。

2.如果你为通用基类编写通用的代码,你会无法享受到类型检查带来的好处。除此之外,碰到某些需要从特殊基类(special base classes)派生出来的类时,你再想要去维护你的代码就会变得难上加难。

3.如果你使用特殊的预处理器,你的代码则会被一些“愚蠢的文本替换机制”(stupid text replacement mechanism)变得不知道范围和类型,这会导致一些非常奇怪的语义错误(semantic errors)。

而模板就是由此而生的。它们是为了那些还没有确定一种或多种类型的函数或模板而被编写出来的。当你使用模板时,类型会作为显式或隐式的参数而被传递。同时又因为模板是C++的语言特性,所以它支持类型检查和范围检查。

在现今的编程中模板十分常见。比如在C++标准库(standard library)中,几乎所有代码都是模板代码。标准库可以提供分类指定类型的实体和值的分类算法,提供管理指定类型元素的数据结构(或称之为容器(container classes)),以及参数化(parameterize)字符类型的字符串等。

无论如何,这只是刚刚开始。模板还允许我们参数化行为,优化代码和参数化信息(parameterize information)。这些相关应用会在后面的章节中提到。我们先从一些简单的模板开始讲起。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ Template》第二版,2017年9月16日出 Templates are among the most powerful features of C++, but they remain misunderstood and underutilized, even as the C++ language and development community have advanced. In C++ Templates, Second Editi on, three pioneering C++ experts show why, when, and how to use modern templates to build software that’s cleaner, faster, more efficient, and easier to maintain. Now extensively updated for the C++11, C++14, and C++17 standards, this new edition presents state-of-the-art techniques for a wider spectrum of applications. The authors provide authoritative explanations of all new language features that either improve templates or interact with them, including variadic templates, generic lambdas, class template argument deduction, compile-time if, forwarding references, and user-defined literals. They also deeply delve into fundamental language concepts (like value categories) and fully cover all standard type traits. The book starts with an insightful tutorial on basic concepts and relevant language features. The remainder of the book serves as a comprehensive reference, focusing first on language details and then on coding techniques, advanced applications, and sophisticated idioms. Throughout, examples clearly illustrate abstract concepts and demonstrate best practices for exploiting all that C++ templates can do. Understand exactly how templates behave, and avoid common pitfalls Use templates to write more efficient, flexible, and maintainable software Master today’s most effective idioms and techniques Reuse source code without compromising performance or safety Benefit from utilities for generic programming in the C++ Standard Library Preview the upcoming concepts feature The companion website, tmplbook.com, contains sample code and additional updates.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值