Suppose I had the following class:
template<typename T>
class Value {
public:
Value(std::string name, T value) : name(name), value(value) { }
T const &value() throw() { return value }
std::string const &name() throw() { return name }
private:
std::string name;
T value;
};
and then I wanted to do something like:
class Group {
public:
Group(std::string name) : name(name) { }
template<typename T>
void add_value(std::string const &name, T const &val) throw() {
Value<T> tmp(name, val);
values.insert(tmp);
return;
}
private:
std::string name;
std::set< Value<????> > values; // HERE BE MY QUESTION
}
Concretely, I want to store Value in its many template forms in the same std::set (like, having a Value<int> and a Value<float> stored in the same set).
You can’t because
Value<T>andValue<X>are completely and totally different and nearly unrelated types, likeintandstring(whenT != X). In your case, they are extremely different because they actually store aTin themselves, which means that if the differentTs are different sizes, theValue<T>s will be different sizes. (That’s not the reason, it’s just a reason.)You may want to try using polymorphism, making
Value<T>inherit from a base class so that you can store pointers to that base class which can be pointing to aValue<T>underneath, whereTis any type your template supports.