访问派生类模板中的基类typedef

Accessing base class typedef in derived class template

本文关键字:基类 typedef 派生 访问      更新时间:2023-10-16

我正试图从派生类模板访问基类中的typedef成员。在模板中,模板参数的名称和基类中typdef的名称相同。

#include <iostream>
using namespace std;
class no {
    public :
    typedef int T;
};
template<typename T> class no1 : public no {
    public :
    T obj;
};
int main() {
    // your code goes here
    no1<string> o ; o.obj = "1";
    return 0;
}
14:24: error: invalid conversion from 'const char*' to 'no::T {aka int}' [-fpermissive]
  no1<string> o ; o.obj = "1";

在上面的代码T中,obj总是int类型。我怎么能强迫obj是template参数,而不是在基类中声明的typdef?

感谢

这对我有效:

template<typename T> class no1;
template<typename T>
struct underlying_type_of;
template<typename T>
struct underlying_type_of<no1<T> >
{
   typedef T type;
};
template<typename T> class no1 : public no {
public :
  typename underlying_type_of<no1>::type obj;
};