Muhammad
Muhammad

Reputation: 1695

using class templates without passing template parameters

// p1: some value
// nt: native type
template<int p1, typename nt>
struct A {};

// pt: pointer to type i.e. int*
// tA: a specialization of A
template<typename pt, typename tA>
struct B1 {};

// pt: pointer to type i.e. int*
// tA: a specialization of A
template<typename pt, typename tA>
struct B2 {};

// tB: specialization of B?
// tA: specialization of A
template<typename tB, typename tA>
struct C {};

// now i want to create a C partial specialization where:
//  A<some_value, native_type>
//  B?<char*, A<some_value, native_type> >

template< template<typename, typename> class B, int p1, typename nt >
struct C< B<char*, A<p1, nt> >, A<p1, nt> > {};

int main()
{
    C< B1, A<10, int> > c;
}

When compiling the above code with clang it gives the error:

error: use of class template B1 requires template arguments
   C< B1, A<10, int> > c;
      ^~

I understand the error and to fix it B1 should be B1<char*, A<10, int> >. Should the compiler deduct the value of these parameters from the most matched specialization?

Upvotes: 2

Views: 730

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126562

Should the compiler deduct the value of these parameters from the most matched specialization?

I'm not sure I understand your question, but if I do, then the answer is "No". The compiler has no idea how to deduce a fully instantiated type (a specialization of B1, in this case) from the bare name of a class template (B1, in this case). You have to specify template arguments for B1.

Keep in mind that specializations of a primary template are selected after you have provided the necessary arguments for the primary template. In this case, your primary template accepts two type parameters, and you have to provide two type arguments.

The fact that you are using a template template parameter B in your template specialization in order to match those type parameters which are instances of B does not change the fact that the parameters of your primary template are two (fully instantiated) types.

Upvotes: 4

Related Questions