0

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?

Update

Looks like this question has been answered before.

Community
  • 1
  • 1
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278

2 Answers2

0

It's standard c++. This is template specialization.In the case you just specialize a method, it's called partial template specialization.
Also, look here

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
0

This is an example of explicit specialization. If there was still a template parameter (but fewer than originally) it would be partial specialization.

Tod
  • 8,192
  • 5
  • 52
  • 93