官术网_书友最值得收藏!

Summary

Both classical polymorphism and generic programming deal with the essential problem of parameterizing the behavior of an algorithm: for example, writing a search function that works with any arbitrary matching operation.

Classical polymorphism tackles that problem by specifying an abstract base class with a closed set of virtual member functions, and writing polymorphic functions that accept pointers or references to instances of concrete classes inheriting from that base class.

Generic programming tackles the same problem by specifying a concept with a closed set of requirements, and instantiating function templates with concrete classes modeling that concept.

Classical polymorphism has trouble with higher-level parameterizations (for example, manipulating function objects of any signature) and with relationships between types (for example, manipulating the elements of an arbitrary container). Therefore, the Standard Template Library uses a great deal of template-based generic programming, and hardly any classical polymorphism.

When you use generic programming, it will help if you keep in mind the conceptual requirements of your types, or even write them down explicitly; but as of C++17, the compiler cannot directly help you check those requirements.

主站蜘蛛池模板: 大同市| 新乡市| 浦东新区| 澄城县| 吉木萨尔县| 阜南县| 西藏| 清苑县| 思南县| 裕民县| 旺苍县| 全南县| 华安县| 曲阳县| 胶州市| 时尚| 内黄县| 治多县| 新巴尔虎右旗| 林周县| 明溪县| 水富县| 姜堰市| 娄底市| 曲水县| 时尚| 沙坪坝区| 磴口县| 浮梁县| 汶川县| 昌黎县| 惠安县| 义乌市| 岱山县| 隆化县| 张家港市| 凤阳县| 吉林省| 穆棱市| 新建县| 钦州市|