7

I have this :

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) );
}

My Compile error is:

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
Behrouz.M
  • 3,445
  • 6
  • 37
  • 64

2 Answers2

6

Unfortunately a template method inside a template class cannot be specialized just based on template argument of method.

You need to specialize the template class also. In other words, the member method specialization should be a complete specialization with respect to class template (i.e. <T,U>) params as well as the member template params (i.e. <size_t>).

For example, you may have to specialize something like this (demo):

template<>  // <------ you have to specialize class also
template<>
inline void A<int, double>::BindValues<1>()  // <-------- see A<T,U>
{
    ...
}
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • @russellsayshi, even I don't have any logical explanation for it. One possibility is to reduce the complexity of the C++ grammar parser, since this feature may not be used so often. – iammilind Apr 13 '15 at 08:36
3

What you are trying to do is partial template specialization for a function, which is not allowed.

You can define the template function on N for a template specialization of A (e.g. A<int, int>::BindValues<N>()) but the other way around is not allowed.

Foo Bah
  • 25,660
  • 5
  • 55
  • 79