Reputation: 33655
I was trying to specialize a class template with another, as in the example below, and I noticed that this compiles and runs (at least on ideone, which is: gcc 4.3.4) whether the commented out 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 it really optional? I was under the impression that for such specialization the template<>
line is required..
Upvotes: 3
Views: 225
Reputation: 76286
That line should not be there.
When doing full specialization, there is template<>
and it's mandatory. When doing partial specialization, you put the parameters of the partial specialization to those angle brackets and there is no additional template
keyword.
So full specialization is:
template <>
struct bar<qyzzy>
and partial specialization is:
template <typename F>
struct bar<foo<F> >
Each has exactly one template
keyword and two pairs of angle brackets.
Upvotes: 3
Reputation: 133014
The line with template <>
should be commented out. Otherwise it should be an error. Bar is a namespace-level template, so in order to specialize it you just write
template <parameters if any>
class bar <arguments>
{
};
I have no idea why gcc accepts when you uncomment the line. I can assure you it most certainly should not
Upvotes: 1
Reputation: 224089
This
template<>
template <typename F>
struct bar<foo<F> >;
should be a syntax error, me thinks.
Upvotes: 3