Nested template specialization results in "Invalid use of explicit template arguments"?

How do I specialize in a nested template? (See the error below.)

using std::reverse_iterator;

template<typename It>
reverse_iterator<It> make_reverse_iterator(const It &it)
{
    return reverse_iterator<It>(it);
}

template<typename It>
It make_reverse_iterator<reverse_iterator<It> >(const reverse_iterator<It> &it)
{
    // Above                     ^
    // error C2768:
    //   'make_reverse_iterator': illegal use of explicit template arguments
    return it.base();
}
+5
source share
2 answers

This is a partial specialization of the function template. This is not allowed.

In this example, you can solve the overload problem:

template<typename It>
It make_reverse_iterator(const reverse_iterator<It> &it)
{
    return it.base();
}

In cases where overloads do not work, you can resort to partial specialization of class templates.

+6
source

This is a specialization of private function templates , which is unacceptable. However, you can achieve the same effect by overloading the function:

template<typename It>
It make_reverse_iterator(const reverse_iterator<It> &it)
{
    return it.base();
}

, make_reverse_iterator, const_reverse_iterator<It>.

+4

All Articles