正在调用模板内typedef的构造函数

Calling constructor of typedef inside template

本文关键字:typedef 构造函数 调用      更新时间:2023-10-16

我在一个模板类中定义了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的构造函数。我该怎么做?

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

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

如果Tick是其他东西,例如函数:,则不需要typename

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