CRTP を使用するときは、ベースを設計するときに注意する必要があります。つまりimpl_get_set
、この場合です。派生クラスが基本特殊化をインスタンス化する場合 (たとえば で行われるA: public impl_get_set<A>
場合)、A
クラスはまだ不完全です。
ただし、メンバー関数宣言でのimpl_get_set
使用の定義。typename T::storage_type
この使用には、完全なT
. これを解決する C++03 の方法は、CRTP ベースがクラス テンプレート パラメーターの一部を必要とする可能性のある関連付けられた型を作成することです。
template<typename Derived, typename StorageType>
struct get_set {
typedef StorageType storage_type;
// It's possible to define those inline as before where
// Derived will be complete in the body -- which is why
// CRTP is possible at all in the first place
storage_type get() const;
void set(storage_type s);
// Convenience for clients:
protected:
typedef get_set get_set_base;
};
struct A: get_set<A, int> {
// Member type is inherited
storage_type data;
};
template<typename T>
struct B: get_set<B<T>, double> {
// Incorrect, storage_type is dependent
// storage_type data;
// First possibility, storage_type is
// still inherited although dependent
// typename B::storage_type data;
// Second possibility, convenient if
// storage_type is used multiple times
using typename B::get_set_base::storage_type;
storage_type data;
void foo(storage_type s);
};
boost::iterator_facade
Boost.Iterator の適切に作成された C++03 スタイルの CRTP ラッパーの良い例です。
C++11 は、関数テンプレートのデフォルト テンプレート引数のおかげで、CRTP ベースを記述する別の方法を提供します。派生クラスのパラメーターを再び依存させることで、完全であるかのように使用できます。つまり、CRTP ベースの特殊化のメンバー関数テンプレートがインスタンス化されたときにのみ検査され、CRTP ベースの特殊化が完了すると検査されます。それ自体は次のとおりです。
// Identity metafunction that accepts any dummy additional
// parameters
template<typename T, typename... Dependent>
struct depend_on { using type = T; };
// DependOn<T, D> is the same as using T directly, except that
// it possibly is dependent on D
template<typename t, typename... D>
using DependOn = typename depend_on<T, D...>::type;
template<typename Derived>
struct get_set {
template<
// Dummy parameter to force dependent type
typename D = void
, typename Storage = typename DependOn<Derived, D>::storage_type
>
Storage get() const
{
// Nothing to change, Derived still complete here
}
};
実際、あなたの例get_set
では、メンバー型が存在するかどうかを気にする必要はありません。
// std::declval is from <utility>
template<
typename D = void
, typename Self = DependOn<Derived, D>
>
auto get() const
-> decltype( std::declval<Self const&>().data )
{ return static_cast<Derived const&>(*this).data; }
この の実装はget
、 への参照を返すという点で独自のセマンティクスとは微妙に異なりますが、これはdata
意図的なものです。