I'm currently working on an AIM client as a summer project, and am writing the OSCAR protocol code.
I can't come up with a good way to represent TLVs (type, length, value) that works correctly with arrays as well as regular integer types. I thought a template class would be the way to do this, as there is always a single value attached to a TLV. This value can be a c-style string, an integer, array of an object, etc. The type is just a tag for the kind of data contained by the value, and the length is the length of that value blob, whatever it may be.
I don't exactly understand what you want to do, however it sounds like you want runtime polymorphism. Templates don't provide that, they only provide compile-time polymorphism. However, you can combine the two concepts by making an abstract class value which provides whatever functions you need, and inherit from it in a concrete template-class. If you know at compile-time which parameterization for the template arguments you will need, you can create them at runtime:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class value
{
...
virtual value* clone() const = 0;
}
template<typename T> class concrete_value : public value
{
...
value* clone const {returnnew concrete_value<T>(*this);
}
void f()
{
std::vector<value*> v;
v.push_back(new concrete_value<constchar*>(...));
// ^^ information needed at compile time: will need template class for "const char*"
}
If you provide an appropriate constructor set, this approach might provide a clan interface. (That is, if your problem is what I think it could be)