2

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.

Community
  • 1
  • 1
sleeparrow
  • 1,242
  • 13
  • 22
  • There is no difference between them - both of them are not declarations. – Constructor Mar 31 '14 at 13:49
  • I'm fairly certain there is indeed a difference, as one will generate a compile error and the other will not for a block of code I have in front of me. I'm also fairly certain that these could be considered declarations. They are declaring template types. – sleeparrow Mar 31 '14 at 13:51
  • No, they don't declare template types. Class template declaration might look like `template class C{/*...*/};` but not like you wrote. – Constructor Mar 31 '14 at 14:20

2 Answers2

3

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*/}
chris
  • 60,560
  • 13
  • 143
  • 205
  • Ah, that makes perfect sense. Looking back at the accepted answer for the linked question, that explained it, too. The wording just threw me off. Thank you! – sleeparrow Mar 31 '14 at 14:02
0

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 */ }
};
Chris Chiasson
  • 547
  • 8
  • 17