typedef typename 是什么?

    xiaoxiao2025-10-27  6

    template<typename T>  class A { public: typedef T a_type; }; template<typename A> class B { public: //typedef A::a_type b_type; typedef typename A::a_type b_type; }; int main() { B<A<int>> b; return 0;

    }

    注意:如果把注释取消,就会产生编译错误。

    必须使用关键字typename的原因是T是一个template 参数。在实例化之前,编译器对T一无所知,因此不知道A::a_type 代表的是一个type或是一个member function或是一个data member,使用typename可以告诉编译器这是一个type使得编译能顺利通过。

    转载请注明原文地址: https://ju.6miu.com/read-1303581.html
    最新回复(0)