开发者

C++ template partial specialization

开发者 https://www.devze.com 2023-01-11 01:39 出处:网络
I cant figure out how to specialize partially this template.compiler complains that template parameter N is not used in partial specialization

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&开发者_如何学Pythonlt;2>::type type;
};

is there more straightforward way?


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号