More C++ Idioms/奇妙に再帰したテンプレートパターン(Curiously Recurring Template Pattern)
出典: フリー教科書『ウィキブックス(Wikibooks)』
目次 |
[編集] 意図
派生クラスの基本クラスを、派生クラス自身をテンプレート引数として特殊化する。
[編集] 別名
- CRTP
- 上からの混入(Mixin-from-above)
[編集] 動機
型には依存しないが、型でカスタマイズ可能な機能を基本クラスに抽出し、派生クラスによってカスタマイズしたインタフェース、属性、動作を派生クラスに混入(mix-in)させる。
[編集] 解法とサンプルコード
典型的には、基本クラステンプレートは、メンバ関数の本体(定義)はその宣言から非常に後になるまでインスタンス化されないという事実を活用する。そして、そのメンバ関数中で派生クラスのメンバ関数を、static_cast経由で利用する。例えば
template <class Derived> struct base { void interface() { // ... static_cast<Derived*>(this)->implementation(); // ... } static void static_interface() { // ... Derived::static_implementation(); // ... } // デフォルトの実装は存在すれば、継承した派生クラスでオーバーライドされるかもしれないし、 // 存在しない場合は、派生クラスでしなければならない(下記参照) void implementation(); static void static_implementation(); }; // 奇妙に再帰したテンプレートパターン(Curiously Recurring Template Pattern (CRTP)) struct derived_1 : base<derived_1> { // このクラスは base 版の implementation を用い、 //void implementation(); // …… static_implementation をオーバーライドする。 static void static_implementation(); }; struct derived_2 : base<derived_2> { // このクラスは implementation をオーバーライドし、 void implementation(); // …… base 版の static_implementation を用いる。 //static void static_implementation(); };
[編集] 関連するイディオム
[編集] Related Idioms
- パラメータ化された基本クラス(Parameterized Base Class)
- Barton-Nackman trick
- 上からの混入(Mixin from above)
- 下からの混入(Mixin from below)