<Effective C++>读书摘要--Templates and Generic Programming<一>

1、The initial motivation for C++ templates was straightforward: to make it possible to create type-safe containers like vector, list, and map. Ultimately, it was discovered that the C++ template mechanism is itself Turing-complete: it can be used to compute any computable value. That led to template metaprogramming: the creation of programs that execute inside C++ compilers and that stop running when compilation is complete.

This chapter won‘t make you an expert template programmer, but it will make you a better one. It will also give you information you need to expand your template-programming boundaries as far as you desire.

<Item 41> Understand implicit interfaces and compile-time polymorphism

2、The world of object-oriented programming revolves around explicit interfaces and runtime polymorphism.The world of templates and generic programming is fundamentally different. In that world, explicit interfaces and runtime polymorphism continue to exist, but they‘re less important. Instead, implicit interfaces and compile-time polymorphism move to the fore.

3、如下代码意味着可以描述w如下两点

  • The interface that w must support is determined by the operations performed on w in the template. In this example, it appears that w‘s type (T) must support the size, normalize, and swap member functions; copy construction (to create temp); and comparison for inequality (for comparison with someNastyWidget). We‘ll soon see that this isn‘t quite accurate, but it‘s true enough for now. What‘s important is that the set of expressions that must be valid in order for the template to compile is the implicit interface that T must support.
  • The calls to functions involving w such as operator> and operator!= may involve instantiating templates to make these calls succeed. Such instantiation occurs during compilation. Because instantiating function templates with different template parameters leads to different functions being called, this is known as compile-time polymorphism.
template<typename T>
void doProcessing(T& w)
{
  if (w.size() > 10 && w != someNastyWidget) {
     T temp(w);
     temp.normalize();
     temp.swap(w);
  }
}

4、An explicit interface typically consists of function signatures, i.e., function names, parameter types, return types, etc.An implicit interface is quite different. It is not based on function signatures. Rather, it consists of valid expressions. Look again at the conditional at the beginning of the doProcessing template:

template<typename T>
void doProcessing(T& w)
{
  if (w.size() > 10 && w != someNastyWidget) {
  ...
}

The implicit interface for T (w‘s type) appears to have these constraints:

  • It must offer a member function named size that returns an integral value.
  • It must support an operator!= function that compares two objects of type T. (Here, we assume that someNastyWidget is of type T.)

Thanks to the possibility of operator overloading, neither of these constraints need be satisfied. Yes, T must support a size member function, though it‘s worth mentioning that the function might be inherited from a base class. But this member function need not return an integral type. It need not even return a numeric type. For that matter, it need not even return a type for which operator> is defined! All it needs to do is return an object of some type X such that there is an operator> that can be called with an object of type X and an int (because 10 is of type int). The operator> need not take a parameter of type X, because it could take a parameter of type Y, and that would be okay as long as there were an implicit conversion from objects of type X to objects of type Y!

Similarly, there is no requirement that T support operator!=, because it would be just as acceptable for operator!= to take one object of type X and one object of type Y. As long as T can be converted to X and someNastyWidget‘s type can be converted to Y, the call to operator!= would be valid.

(As an aside, this analysis doesn‘t take into account the possibility that operator&& could be overloaded, thus changing the meaning of the above expression from a conjunction to something potentially quite different.)

5、The implicit interfaces imposed on a template‘s parameters are just as real as the explicit interfaces imposed on a class‘s objects, and both are checked during compilation.

6、Things to Remember

  • Both classes and templates support interfaces and polymorphism.
  • For classes, interfaces are explicit and centered on function signatures. Polymorphism occurs at runtime through virtual functions.
  • For template parameters, interfaces are implicit and based on valid expressions. Polymorphism occurs during compilation through template instantiation and function overloading resolution.

2、

时间: 2024-10-12 20:29:11

<Effective C++>读书摘要--Templates and Generic Programming<一>的相关文章

Effective STL读书摘要(一)

一直在用STL,认为对STL也有一些理解,比如比较函数怎么写,什么情况下用什么容器效率高,但是当你读过Effective STL之后才知道这远远不够,之前的代码还有很多可以优化的空间,下面我会罗列一些映像比较深的点,比较偏向代码因为这样可以方便以后的调用.这里是到Item29,余下的留下次看. 1) 检查容器是否为空 if(c.empty()){}   better than if(c.size()==0){} 2)如果能用批量操作函数就不要用循环来做 批量操作可以提高效率,要有能用批处理尽量批

&lt;Effective C++&gt;读书摘要--Resource Management&lt;一&gt;

1.除了内存资源以外,Other common resources include file descriptors, mutex locks, fonts and brushes in graphical user interfaces (GUIs), database connections, and network sockets. Regardless of the resource, it's important that it be released when you're fini

&lt;Effective C++&gt;读书摘要--Designs and Declarations&lt;三&gt;

<Item 22> Declare data members private 1.使数据成员private,保持了语法的一致性,client不会为访问一个数据成员是否需要使用括号进行函数调度,还是不使用括号直接访问成员而纠结. 2.使数据成员private,if you use functions to get or set its value, you can implement no access, read-only access, and read-write access. Heck

&lt;Effective C++&gt;读书摘要--Inheritance and Object-Oriented Design&lt;一&gt;

1. 3.

&lt;Effective C++&gt;读书摘要--Designs and Declarations&lt;二&gt;

<Item 20> Prefer pass-by-reference-to-const to pass-by-value 1.By default, C++ passes objects to and from functions by value (a characteristic it inherits from C). Unless you specify otherwise, function parameters are initialized with copies of the

&lt;Effective C++&gt;读书摘要--Ctors、Dtors and Assignment Operators&lt;二&gt;

<Item 9> Never call virtual functions during construction or destruction 1.you shouldn't call virtual functions during construction or destruction, because the calls won't do what you think, and if they did, you'd still be unhappy. If you're a recov

&lt;Effective Django&gt;读书笔记

In Django parlance, a project is the final product, and it assembles one or more applications together. Manage.py is a pointer back to Django-admin.py with an environment variable set, pointing to your project as the one to read settings from and ope

Effective C++读书笔记 Part1

##Effective C++ Notes ### Part I. Accustoming Yourself to C++ ####1. View C++ as a federation of languages- C++ is a multi-paradigm programming language with paradigms including procedure oriented, object oriented and generic programming with their o

Effective Objective-C 读书笔记

一本不错的书,给出了52条建议来优化程序的性能,对初学者有不错的指导作用,但是对高级阶段的程序员可能帮助不是很大.这里贴出部分笔记: 第2条: 使用#improt导入头文件会把头文件的内容全部暴露到目标文件中,而且如果两个类之间存在循环引用则会出现编译错误,所以要尽量使用@class进行类声明. 如果需要实现一个协议,则必须#improt这个协议的头文件,所以可以将协议单独定义在一个.h文件当中.如果这个协议是代理模式协议的一部分,即需要与类捆绑使用才有实际意义,则建议定义在类当中,并以类名为前