《C++ Template. The Complete Guide》笔记之一 缘起

从面对《Modern C++ Design》中backEnds.template的用法一无所知开始,要彻底搞清楚C++的模板是如果工作的决心其实已经埋下了。更何况今天在实现B树的时候反复被模板的编译问题折磨着。更加坚定了看完这本《C++ Template. The Complete Guide》的信念。尽管过去对这个主题知道一些东西,什么特化,偏特化,参数演绎等等。但是系统地看一看也是有必要的。当然在这里学习的时候还是非常同意Nocolai的观点,实用才是王道,因为我不是科学家也不可能成为科学家:)。

 

在这里学习的开始,还是乐意重复一遍那个经典的主题:Why Templates?

理由很简单,在设计数据结构或者算法的时候,需要对不同的数据类型,但是相似的行为做描述。比如说你有一个QuickSort,你想让他能对int,float, std::string都能够排序。

你有很多办法:第一就是你实现3个QuickSort, 参数分别是int[],float[],std::string[],这样是可以工作的。但是带来的问题显而易见,如果你第一次实现的int的排序是错的,那你完蛋了,你必须把这三个实现都改了。如果这个代码是你一个人维护那勉强可以,如果很多人维护,别人可不知道有这样的历史在代码里面。所以这个方案是不妥当的。

第二个方法,其实是比较常见的,就是void*或者Object来取代具体的类型,每次我实现一个容器的时候push_back的内容都是Object&或者void*,用这个Object&或者void*来代替了具体的类型。这样的好处是确实你不用重写三分代码了,但是问题一样很明显,用过了void*以后需要类型转化,原先的类型信息丢失了。在这样的情况下,一个float也被类型转化成了int加入到排序的队列中,而这确实不显示你的想法。如果Object&也一样有问题,如果待排序的数据ObjectA同时继承与ObjectB和ObjectC,而这两个父类又都是Object的子类,那样那个讨厌的菱形有出现了!!!简单的说这种对待排序参数强制的要求在维护性上是得不到很好的保证的。

当然,在模板没有应用的时候,还有人用了宏定义来取代模板。让我们来看看那些曾经熟悉的身影:

如果不是那些C/C++的大师不断强化这个MAX或者MIN的写法,我想这种"stupid replacement mechanism"肯定会让很多人抓狂。是的,这样的宏定义太容易出错了,在C++里面,他不断的被取代,简单的函数变成了inline,值define编程了enum。总之C++的基调就是忘记宏,忘记预定义。

所以在这样的时刻,在这个C#和Java都在推动template的时刻。我们还有什么理由不用C++的模板呢?

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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.
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 Edition, 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.
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、付费专栏及课程。

余额充值