Here I define a class template Foo, specialize its member function, and then supply a partial specialization for that class:
// this is Foo_0
template<typename T, typename S>
class Foo {
public:
void operator()() {
std::cout << "Foo_0\n";
}
template<typename R>
void bar(R) {
std::cout << "I'm from Foo_0\n";
}
};
template<>
template<>
void Foo<double, int>::bar(double) {
std::cout << "Now I'm specialized!\n";
}
// this is Foo_1
template<typename T>
class Foo<T, int> {
public:
void operator()() {
std::cout << "Foo_1\n";
}
};
And I instantiate Foo on VS2015 like this:
Foo<double, int> f;
f();
Surprisingly, f() prints "Foo_0", which means the partial specialized template Foo_1 isn't chosen. What's even stranger, when I comment the specialization of Foo::bar(double), f() prints "Foo_1"!
Then I test this:
Foo<int, int> f;
f();
This time, f() also prints "Foo_1", the class specialization is applied.
So it seems that the specialization of member bar() do influence the application of partial specialization of the class template. Is that true? Why does it work like this?
error: partial specialization of 'class Foo<T, int>' after instantiation of 'class Foo<double, int>' [-fpermissive]
) – Jarod42bar
cause an implicit instantiation ofFoo<double, int>
, which is a better match thanFoo<T, int>
so you getFoo_0
with the specializedbar
andFoo_1
when you comment it out. – TemplateRex