Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

templates

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();
}
like image 411
user541686 Avatar asked Mar 24 '12 09:03

user541686


1 Answers

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.

like image 96
R. Martinho Fernandes Avatar answered Nov 14 '22 21:11

R. Martinho Fernandes