Let's say I have a base template function foo<T>()
, with a full specialization for T = int
. The body of the base template for foo<T>()
invokes a another template, Bar<T>::baz
, but foo<int>()
does not.
If foo<int>()
is written, the compilation fails if Bar<int>
doesn't have a member baz
! Why is this? We never call the base template, and can't because it's masked by the specialization! This is confirmed if the reference to Bar
is removed from the base template; compilation succeeds and we see that the specialized version of foo()
is indeed called as expected.
foo.h
#include <iostream>
template <typename T>
class Bar {};
template <typename T>
void foo(T arg) {
Bar<T>::foo(); // XXX: error; comment to fix
std::cout << "using base template" << std::endl;
}
foo.cpp
#include "foo.h"
template <> void foo<int>(int i) {
std::cout << "specialization" << std::endl;
}
main.cpp
#include "foo.h"
int main(int argc, char **argv) {
int i = 1;
foo<int>(i); // prints "specialization" if noted line is commented
return 0;
}
Why does this fail to compile?