Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template template parameter with wrong number of template parameters

Tags:

c++

templates

Consider a template class C with a policy set via 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
}

PolicyTwo doesn't work because of wrong number of template arguments. Is there a way to use PolicyTwo as POLICY template parameter if you specify the types for the additional template parameters?

I'm using C++03, so alias declarations are not available. I'm aware of this question, but I don't see a solution to my problem there.

like image 275
Gabriel Schreiber Avatar asked Oct 22 '22 12:10

Gabriel Schreiber


1 Answers

Depending on how the policy is used, you may be able to manage with inheritance in place of alias templates:

template<int U, int V> struct PolicyTwoAdaptor {
  template<class T> struct type: PolicyTwo<T, U, V> { }; };
C<int, PolicyTwoAdaptor<1, 2>::type> mc2;
like image 70
ecatmur Avatar answered Nov 04 '22 18:11

ecatmur