I've marked an answer as "correct",
but I'm not really satisfied. I'm not
asking HOW it works; I'm asking why it
was designed that way. The way it
works doesn't strike me as consistent
with OO. It does even seem consistent.
Why does function template
specialization have the restriction
that the interface must be consistent,
while class template specialization
does not??
If that was your question, you probably should have said so to begin with.
I'm going to start with the last bit: why functions require identical signatures. That ones easy, functions already have a way to reuse names via overloading.
int f(int);
double f(double); //no templates needed
Allowing a specialization to change the function's signature would needlessly complicate the already difficult process of name resolution, without actually providing any additional functionality.
In the case of class types, a template is much more complicated. Not only do you have member functions to consider, you have member objects, typedefs, nested types, and so on. Specialization specializes all of those. If SomeTemplatedObject<double>
requires a particular private member that SomeTemplatedObject<T>
in general doesn't, how can you specialize SomeTemplatedObject
for double
without changing the "interface"? Or what of methods that don't make sense for certain types? Or nested types that need to be modified in order to keep a consistent interface? And that's not even touching on the larger issue of template metaprogramming.
You don't even need to have a complete type for the general case.
template<typename T>
struct Object;
template<>
struct Object<int> { //what interface does this need?
typedef int type;
};
If you feel compelled to adhere to some purist definition of OO, then you are always free to just not specialize templates without precisely matching their original layout.