I'm attempting to declare a Row
and a Column
class, with the Row
having a private std::map
with values pointing to a templated Column
. Something like this:
template <typename T>
class DataType {
private:
T type;
};
template <typename T>
class Field {
private:
T value;
DataType<T> value;
};
class Row {
private:
std::map<unsigned long,Field*> column;
};
Well, I suppose in principle the Row
class shouldn't have to know which kind of Field
(or Column
) we'd like to use, i.e. whether it's a Field<int>
in column 1 or a Field<double>
in column 2. But I'm not sure what's the correct syntax for the Row::column
declaration, or if the std::map
is limited in this sense and I should be using something else.
I appretiate you suggestions and thank you for them in advance.
A
Row< int, float, int>
is really different from aRow<int, std::string>
. Clearly,Row<int,float,int>.field<0>
should be aField<int>
whileRow<int,float,int>.field<1>
should be aField<float>
. AndRow<int,float,int>.field<3>
is a compiler error.The easiest way to do so is using Boost. A whole lot of the intelligence was pioneered by Loki (see Modern C++ Design, by Andrei Alexandrescu) but Boost is more modern and better supported.
Normally, you wouldn't iterate over the fields - each field has its own type. But of you do, you would indeed need a
FieldBase
. If you need such an interface, it's probably worthwhile to also store the fields internally as aboost::array<FieldBase, N>
(i.e.Row<int,float,int>
has aboost::array<FieldBase, 3>
). You should never need todynamic_cast
thatFieldBase*
, though. That is a runtime test, and you always know the exactT
of eachField<T>
at compile time.Field
alone is not a type, but a template which can generate a family of types, such asField<int>
andField<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: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 aField<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 usingBut 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:
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
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.