在模板中调用typedef的构造函数

时间:2015-02-06 23:22:45

标签: c++ templates typedef

我在模板类中定义了typedef:

template <class T>
class ROIAlg{
  public:
    typedef std::vector<T> Waveform;
    typedef typename Waveform::const_iterator Tick;
    typedef std::pair<Tick, Tick> Region;
  //etc.
};

和另一个使用它们的类

template <class T>
class WaveformPropertiesAlg{
  public:
    WaveformPropertiesAlg();
  private:
    typename ROIAlg<T>::Region fCPR;
  //etc.
};

WaveformPropertiesAlg的构造函数的实现:

template <class T>
WaveformPropertiesAlg<T>::WaveformPropertiesAlg():
  fCPR(ROIAlg<T>::Tick(), ROIAlg<T>::Tick()) {
  //etc
}

另一段代码尝试构造WaveformPropertiesAlg<short int>但编译失败:

In instantiation of ‘WaveformPropertiesAlg<T>::WaveformPropertiesAlg() [with T = short int]’:
RawDigitAndWireComparisonAlg.cxx:13:77:   required from here
WaveformPropertiesAlg.h:30:51: error: dependent-name ‘ROIAlg<T>::Tick’ is parsed as a non-type, but instantiation yields a type
   fCPR(ROIAlg<T>::Tick(),ROIAlg<T>::Tick())
                       ^
WaveformPropertiesAlg.h:30:51: note: say ‘typename ROIAlg<T>::Tick’ if a type is meant

我不认为这里的类型是因为我正在调用Tick的构造函数。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

typedef typename Waveform::const_iterator Tick;&lt;&lt;这是一种类型,因此编译器告诉您需要typename

fCPR(typename ROIAlg<T>::Tick(), typename ROIAlg<T>::Tick())

您不需要typename的情况是Tick是其他内容,例如函数:

template <class T>
class ROIAlg{
  public:
    static T Tick();
};