0

Sometimes I see class specialization code like this:

template <>
template < typename T >
class Foo<Foo2<T>>
{
...
};

For classes like this:

template < typename T > class Foo {};

template < typename T > class Foo2 {};

I know what this code means, but my question is: what is the use of “template <>” in the class specialization given that the same thing can be done without it:

template <typename T> Foo<Foo2<T>> {};

Is there any case, not limited to class specialization, where this syntax is necessary? (template followed by another template, not template template parameters)

yggdrasil
  • 737
  • 5
  • 14
  • 1
    11, 14 **and** 17, really? – StoryTeller - Unslander Monica Sep 25 '18 at 05:21
  • 2
    Also, can you make your code less pseudo? It's not valid C++, so it's not good indication of what you may or may have not have seen. A [mcve] will get you your answer much faster. – StoryTeller - Unslander Monica Sep 25 '18 at 05:26
  • Not sure if duplicate, but probably relevant: [What is the meaning of template<> with empty angle brackets in C++?](https://stackoverflow.com/q/6288812/9199167) – Max Vollmer Sep 25 '18 at 05:29
  • Sorry I've fixed the code. The same usage of template<> is in the accepted answer here: https://stackoverflow.com/questions/4189945/templated-class-specialization-where-template-argument-is-a-template/4200397#4200397 – yggdrasil Sep 25 '18 at 05:54
  • 1
    I'm pretty certain the case in the example you bring (and re-iterate in your post) is a typo, since the code itself is ill-formed. It's not that the same thing *can* done without it, it *needs* to be done without it. I'm voting to close as non-reproducible, typo. – StoryTeller - Unslander Monica Sep 25 '18 at 06:01
  • @StoryTeller Thanks for clearing that! So the first part of my question makes no sense. Regarding the other question: "Is there any case where this syntax is necessary?", looking at the answer here: https://stackoverflow.com/questions/30824473/template-template-parameter-partial-specialization-c Answering the question myself, I think the only case in when defining a templated function in a templated class. – yggdrasil Sep 25 '18 at 06:13
  • That I think is covered by the Max Vollmer's suggested duplicate. Templates can have members that are themselves templates. What Barry shows there is a full specialization for a member template that is inside a fully specialized template. So two templates means twice the `template<...>` fun. – StoryTeller - Unslander Monica Sep 25 '18 at 06:17
  • @StoryTeller That makes sense, thanks! Not sure what I should do with this question. Maybe answer it myself stating the first part is incorrect or rewrite it to the second question only? Or have Max Vollmer mark it as a duplicate. – yggdrasil Sep 25 '18 at 06:31
  • @A.S. - It's entirely up to you, depends on how much effort you want to put into it. There's also the option of deleting it entirely if you think it's not worth the hassle at the end of the day. – StoryTeller - Unslander Monica Sep 25 '18 at 06:34

1 Answers1

0

Answering my own question (Thanks to Story Teller and Max Vollmer for the tips), this code is invalid:

template <>
template < typename T >
class Foo<Foo2<T>>
{
...
};

While, regarding the use of the template <...> template <...> syntax, it is necessary for the definition of a templated method inside a templated class. E.g:

template < T > template < U > void Foo<T>::function() { }
yggdrasil
  • 737
  • 5
  • 14