tags:

views:

98

answers:

1

I cant figure out how to specialize partially this template. compiler complains that template parameter N is not used in partial specialization

#include <boost/multi_array.hpp>

template<typename T, class A>
struct adaptable;

template<typename T, size_t N>
struct adaptable<T,
                 // line below is the problem
                 typename boost::multi_array<T,N>::template array_view<2>::type>
{
    typedef typename boost::multi_array<T,N>::template array_view<2>::type type;
};

I can add dummy template parameter just to silence compiler.

template<typename T, class A, class A0 = A>
struct adaptable;

template<typename T, size_t N>
struct adaptable<T,
                 typename boost::multi_array<T,N>::template array_view<2>::type,
                 boost::multi_array<T,N> >
{
    typedef typename boost::multi_array<T,N>::template array_view<2>::type type;
};

is there more straightforward way?

+1  A: 

I don't see anything in your example that looks like partial specialization. A partial specialization is a specialization that specifies exact types for some if the base template parameters, but leaves others open. For example:

template <class T, class U>
struct my_template {    
     // the base template where both T and U are generic
};

template <class T>
struct my_template<int> { 
    // A partial specialization where T is still generic, but U == int
};

To support partial specialization, the base template has to have at least two template parameters (call the number N). The partially specialized template can have 1..N-1 template parameters. The partial specialization must be located where the compiler will already have "seen" the base template before attempting to compile the partial specialization. The partial specialization is written as a completely separate template from the base template (though the base template and all specializations must have the same name, of course).

Jerry Coffin
what is the name for this `template <class T> class my_template<other_template<T> >`
aaa
@aaa: That's just a class template that uses a specialisation of another class template.
j_random_hacker
@j_r is there proper name for this?
aaa
@aaa: Not to my knowledge.
j_random_hacker
Your partial specialization is incorrect, you need to provide all the parameters, so it should be `template <class T> struct my_template<T, int> {};`.
Luc Touraille
Moreover, I think your definition of partial specialization is not correct: you don't necessarily specify an exact type for a parameter, and the number of template parameters of the specialization can be equal or superior to the original number of parameters. Examples : `template <typename T> struct foo; template <typename U> struct foo<U *> {}; // partial specialization for pointers template <typename T, typename U> struct foo<std::pair<T, U> > {}; // partial specialization for std::pair (notice that we have two template parameters)`
Luc Touraille
@Luc: Your `pair<T, U>` example was helpful, thanks. I think Mike Dinsdale's comment at the top is actually the "right answer".
j_random_hacker