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

non*_*ame 6 c++ templates

我试图从派生类模板访问基类中的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";
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,T是obj,总是int类型.如何强制obj是模板参数而不是基类中声明的typdef?

谢谢

小智 0

这对我有用:

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;
};
Run Code Online (Sandbox Code Playgroud)