プロキシポリシーは、自身と同じインタフェースを持つポリシークラスを実装するために用いるポリシークラスである。実装に用いるポリシークラスは、固定のクラスでもよいし、テンプレートパラメータで受け取ってもよい。以下に後者の例を示す。
例 5.13. テンプレートパラメータを用いたプロキシポリシー
template <typename T>
class allocation_policy_new_backend
{
protected:
void* allocate() { return ::operator new(sizeof(T)); }
void deallocate(void* p) throw() { ::operator delete(p); }
};
struct allocation_policy_new
{
template <typename T>
struct bind { typedef allocation_policy_new_backend<T> type; };
};
template <typename T, class AP>
class allocation_policy_purged_backend
: public AP::template bind<T>::type
{
private:
typedef typename AP::template bind<T>::type inherited;
protected:
using inherited::allocate;
void deallocate(void* p) throw()
{ inherited::deallocate(p); std::memset(p, 0, sizeof(T)); }
};
template <class AP>
struct allocation_policy_purged
{
template <typename T>
struct bind { typedef allocation_policy_purged_backend<T, AP> type; };
};
template <typename T, class AP>
class container
: public AP::template bind<T>::type
{
// ...
};
void foo()
{
typedef int T;
typedef allocation_policy_new AP_new;
typedef allocation_policy_purged<AP_new> AP_purged;
container<T, AP_purged> C;
// ...
}
allocation_policy_newは通常の割り当てポリシーである。allocation_policy_purgedは、実装に用いるポリシークラスをテンプレートパラメータとして受け取るプロキシポリシーであり、例えばallocation_policy_newを実装に用いることができる。
allocation_policy_newと実体化後のallocation_policy_purgedは同じインタフェースを持つので、ホストクラスcontainerは両者を区別せずに扱うことができる。