Template template parameter with the wrong number of template parameters

Consider a template class C with a set of policies through a template template parameter and two policy definitions:

template<class T> struct PolicyOne { };
template<class T, int U, int V> struct PolicyTwo { };
template<class T, template<class> class POLICY> struct C { POLICY<T> policy; };

void f()
{
    C<int, PolicyOne> mc1;
    C<int, PolicyTwo<1, 2> > mc2; // doesn't work this way
}

PolicyTwodoes not work due to the wrong number of template arguments. Is it possible to use a parameter PolicyTwolike POLICYtemplate if you specify types for additional template parameters?

I use C ++ 03, so alias declarations are not available. I know this question , but I do not see a solution for my problem.

+5
source share
2 answers

Depending on how the policy is used, you can manage with inheritance instead of alias patterns:

template<int U, int V> struct PolicyTwoAdaptor {
  template<class T> struct type: PolicyTwo<T, U, V> { }; };
C<int, PolicyTwoAdaptor<1, 2>::type> mc2;
+3
source

hwo , , ( , ):

template <typename T> struct PolicyBase { typedef T value_type; };
template<class T> struct PolicyOne : public PolicyBase<T> { };
template<class T, int U, int V> struct PolicyTwo : public PolicyBase<T> { };
template<class POLICY> struct C { POLICY policy; typedef typename POLICY::value_type T; };

void f()
{
    C<PolicyOne<int> > mc1;
    C<PolicyTwo<int, 1, 2> > mc2; // doesn't work this way
}

, tempalte . typedef ( ).

0

All Articles