sleeparrow
sleeparrow

Reputation: 1279

What is the difference between two template types and two template parameter lists?

What is the difference between these two declaractions?
template<typename T, typename U>
template<typename T> template<typename U>

This was vaguely explained in the accepted answer on this question: "too many template-parameter-lists" error when specializing a member function

I understand that they are not the same thing, but I'm having trouble finding resources that teach usage of the latter. Explanation and examples would be much appreciated.

Upvotes: 2

Views: 295

Answers (2)

Chris Chiasson
Chris Chiasson

Reputation: 816

This can also happen if you specialize a class template with another type template. See, for example, how MyTemplateClass is specialized on a type SomeRandomClass in this answer: https://stackoverflow.com/a/4200397

Quoting in case it changes:

template <>
template <typename T,typename S>
class MyTemplateClass <SomeRandomClass<T,S> >
{
    void DoSomething(SomeRandomClass<T,S>& t) { /* something */ }
};

Upvotes: 0

Qaz
Qaz

Reputation: 61920

Consider a function template that needs two types. This would require two parameters:

template<typename T, typename U>
bool isEqual(const T &t, const U &u) {
    return t == u;
}

Now consider a class template with a member function template. This would require two lists:

template<typename T>
struct Foo {
    template<typename U>
    void bar(const U &u);
};

template<typename T>
template<typename U>
void Foo<T>::bar(const U &u) {/*do something with u*/}

Upvotes: 3

Related Questions