user541686
user541686

Reputation: 210755

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

How do I specialize a nested template? (See 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();
}

Upvotes: 5

Views: 3538

Answers (2)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234654

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

You can solve the problem in this example with an overload instead:

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

In cases where overloads don't work you can resort to partial specialization of class templates.

Upvotes: 6

Jon
Jon

Reputation: 437854

This is a partial function template specialization, which is not allowed. You can however achieve the same effect by overloading the function:

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

The above is not a template specialization, but a template overload of make_reverse_iterator where the parameter is a const_reverse_iterator<It>.

Upvotes: 4

Related Questions