模板與范型編程
C++ template 演變:
type-safe(類型安全, 主要指容器) -> generic programing(范型編程, 如 STL 算法) -> template metaprograming(模板元編程, 即利用編譯期生成代碼, 或稱為代碼運(yùn)行在編譯期內(nèi)的方式)
條款41
了解隱式接口和編譯期多態(tài)
隱式接口就是讓表示式必須成立(成立即可)所滿足的條件, 并非依靠函數(shù)簽名式(包含函數(shù)名稱, 參數(shù)類型和返回類型).
編譯期多態(tài)指在編譯期"以不同的 template 參數(shù)具現(xiàn)化 function templates".
classes 和 templates 都支持接口(interface)和多態(tài)(polymorphism).
對(duì)于classes, 接口是顯式的, 以函數(shù)簽名為中心, 多態(tài)則是通過 virtual 函數(shù)發(fā)生于運(yùn)行期.
對(duì)于templates, 接口是隱式的, 基于有效表達(dá)式, 多態(tài)則是通過 template 具現(xiàn)化和函數(shù)重載解析發(fā)生于編譯期.
條款42
了解 typename 的雙重意義
聲明 template 參數(shù)時(shí), class 和 typename 可以互換.
請(qǐng)使用關(guān)鍵字 typename 標(biāo)識(shí)嵌套從屬類型(目的)名稱, 但不得在 base classes lists(基類列)或 member initialization list(成員初值列)內(nèi)以它作為 base class 修飾符.