C++ std::map of template-class values

Field alone is not a type, but a template which can generate a family of types, such as Field<int> and Field<double>. All these fields are not related such that the one is somehow derived from the other or such. So you have to establish some relation between all these generated types. One way is to use a common non-template base class:

class FieldBase { };

template <typename T>
class Field : public FieldBase {
  private:
    T value;
    DataType<T> type;
};
class Row {
  private:
    std::map<unsigned long,FieldBase*> column;
}; 

And consider using smart pointer instead of that raw pointer in the code. Anyway, now the problem is that the type-information is lost - whether you point to a Field<double> or to a Field<int> is not known anymore and can only be detected by keeping some sort of type-flag in the base which is set by the templated derived class - or by asking RTTI using

dynamic_cast<Field<int>*>(field) != 0

But that's ugly. Especially because what you want there is a value semantic. I.e you would want to be able to copy your row, and it would copy all the fields in it. And you would want to get a double when a double is stored - without first using RTTI to hack your way to the derived type.

One way of doing it is to use a discriminated union. That is basically an union for some arbitrary types and in addition a type-flag, which stores what value is currently stored in that field (e.g whether a double, int, ...). For example:

template <typename T>
class Field {
  private:
    T value;
    DataType<T> type;
};
class Row {
  private:
    std::map<unsigned long, 
             boost::variant< Field<int>, Field<double> > > 
      column;
};

boost::variant does all the work for you. You can use visitation to make it call a functor using the right overload. Have a look at its manual


  1. You got an error there: you have to "value" member in Field (one should probably be "type").
  2. Please don't keep raw pointers in the map's value. Use boost::shared_ptr.
  3. Also, you should have a good reason for writing such classes where there are plenty of DB/table handling code out there already which you can probably use. So, if it's applicable, consider using something existing and not writing your own table handling code.

Now, to answer your question :), the Field<> classes can inherit from a common base class that's shared by all data types. This way a container such as your column map can keep pointers (make that shared pointers) to derived objects that are instanced of a template class.