You could try using template specialization
in addition to the code you gave, to optimize the most common cases (this is how, for example, vector<bool>
can be more space-efficient).
For example:
template<>
string toString<char>(char sAttrValue) {
...
}
template<>
string toString<int>(int sAttrValue) {
...
}
And so on for the other types, each one using a conversion method specific and optimized for that type. Any type that doesn't match one of these specialized templates will fall back to the default stringstream
method.