Possible Duplicate:
template class member function only specialization
If I want to specialise just one method in a template, how do I do it?
Today a colleague demonstrated this form of specialization to me (compiled with GCC):
#include <iostream>
#include <sstream>
template<typename T>
struct Item {
Item(const T & value) : value(value) {}
std::string toString() const {
std::stringstream ss;
ss << value;
return ss.str();
}
T value;
};
template<>
std::string Item<char>::toString() const {
return "I am a character.";
}
int main() {
Item<int> i(1);
std::cout << "i: " << i.toString() << std::endl;
Item<char> c('c');
std::cout << "c: " << c.toString() << std::endl;
}
I always thought there was only class specialization (full or partial) and function specialization.
Is the above standard C++? If yes, then what is the name of this kind of specialization?