关于本章
C++ Core Guidelines: Templates and generic programming
NOTE: 非常值得读
Summary
下面对本章的idiom进行了总结:
template metaprogramming中核心的idiom:
template metaprogramming的设计思想:
简化代码的技巧:
idiom | example | summary |
---|---|---|
Object Generator | std::make_pair、std::make_shared | 充分运用function template的automatically deduce parameter特性,进而实现简化代码的目的 |
Return Type Resolver | std::set<int> random_s = getRandomN(10); |
提供**templatized conversion operator function**,充分运用compiler自动地调用**conversion operator function**来讲将**temporary object**转换为目标变量(即等号左侧的变量)从而达到简化代码的目的 |
nullptr | 对Return Type Resolver的应用 |
template alias:
idiom | example | summary |
---|---|---|
Type Generator | Directory<int>::type |
其实就是Templated Typedef,c++11的using,即Template aliases能够简化type generator。 |
reflection:
idiom | example | summary |
---|---|---|
Member Detector | 实现Compile-time reflection capabilities | |
OOP:
idiom | idiom using it | summary | |
---|---|---|---|
Curiously Recurring Template Pattern | Non-copyable Mixin Calling Virtuals During Initialization |
Mixin-from-above | Specialize a base class using the derived class as a template argument,从而实现Static polymorphism |
Parameterized Base Class | Mixin-from-below | To abstract out an aspect in a reusable module and combine it in a given type when required. | |
Barton-Nackman trick | 已经过时,现代compiler已经支持了。 |