I have a class that consists of an inner template class (iterator
) with enumeration (RVSignalInfo::DataType
) parameter and template function (getIterator
) related to this inner class:
class ProviderSignalPlotCurve : public QObject
{
// ...
template <RVSignalInfo::DataType DT>
class iterator
{
iterator(ProviderSignalPlotCurve* curve, quint64 begin, quint64 end) throw(WrongReadonlyPointerTypeException) {
throw WrongReadonlyPointerTypeException();
}
};
template<RVSignalInfo::DataType DT>
inline iterator<DT> getIterator(quint64 begin, quint64 end) {
return iterator<DT>(this, begin, end);
}
};
template<>
class ProviderSignalPlotCurve::iterator < RVSignalInfo::DataType::Float >
{
public:
iterator operator++() { ... }
iterator operator++(int) { ... }
// ...
private:
iterator(ProviderSignalPlotCurve* curve, quint64 begin, quint64 end)
: m_curve(curve), m_index(begin)
{
// ...
}
// ...
};
template<>
class ProviderSignalPlotCurve::iterator < RVSignalInfo::DataType::Double >
{
public:
iterator operator++() { ... }
iterator operator++(int) { ... }
// ...
private:
iterator(ProviderSignalPlotCurve* curve, quint64 begin, quint64 end)
: m_curve(curve), m_index(begin)
{
// ...
}
// ...
};
// ...
Each enumeration member has its own specialization of template class.
If I try to create an instance of iterator
, the compiler provides an the value of 'dataType' is not usable in a constant expression
error. I guess it's because the template parameter has to be defined in runtime. Is there a way to deal with this problem?
RVSignalInfo::DataType dataType = curve->signalInfo()->dataType();
auto iterator = curve->getIterator<dataType>(visiblePartBegin, visiblePartEnd);
I found this workaround with switch
statement, but it doesn't look convenient because it'll have to change the switch
statement every time I add or remove an enumeration member. Is there more generalized way to deal with it?