Is `template <>` optional when specializing a class template using another class template?

I tried to specialize the class template with another, as in the example below, and I noticed that this compiles and runs (at least on ideone, namely: gcc 4.3.4), whether the commented line is commented out or not.

#include <iostream>
template <typename F>
struct foo{
  typedef F value_type;
};

template <typename G>
struct bar{};

//template<>  <- works if commented out or not
template <typename F>
struct bar<foo<F> >
{
  typename foo<F>::value_type val;
};

int main(void)
{
  typedef foo<int> F;
  typedef bar<F> B;
  B b;
  b.val = 10;
  std::cout << b.val << std::endl;
  return 0;
};

So my question is: is this really optional? I got the impression that a line is required for such specialization template<>..

+3
source share
3 answers

This line should not contain .

template<>, . template.

, :

template <>
struct bar<qyzzy>

:

template <typename F>
struct bar<foo<F> >

template .

+3

template<>
template <typename F>
struct bar<foo<F> >;

, .

+3

template <> . . - ,

template <parameters if any>
class bar <arguments>
{
};

, gcc , . , , ,

+1

All Articles