模板类成员函数的显式特化

explicit specialization of template class member function

本文关键字:函数 成员      更新时间:2023-10-16

我有这个:

template<class T, class U>
class A
{
    template<size_t N>
    void BindValues();
}
template<class T, class U>
template<size_t N>
inline void A<T, U>::BindValues()
{
    conn->setValue<N-1>( std::get<N-1>(m_Tuple) );
    BindValues<N-1>(conn);
}
template<class T, class U>
template<>
inline void A<T, U>::BindValues<1>()
{
    conn->setValue<0>( std::get<0>(m_Tuple) );
}

我的编译错误是:

invalid explicit specialization before '>' token
enclosing class templates are not explicitly specialized
template-id BindValues<1> for void A<T, U>::BindValues() does not match any template declaration

不幸的是,template类中的template方法不能仅仅基于方法的template参数进行专门化。

还需要专门化template class 。换句话说,对于class template(即<T,U>)参数以及成员template参数(即<size_t>),成员方法专门化应该是完全专门化

例如,您可能需要专门化如下(demo):

template<>  // <------ you have to specialize class also
template<>
inline void A<int, double>::BindValues<1>()  // <-------- see A<T,U>
{
    ...
}

您要做的是对函数进行部分模板专门化,这是不允许的。

你可以在N上为a的模板专门化(例如A<int, int>::BindValues<N>())定义模板函数,但不允许其他方式。