[C ++] The difference between typedef and typedef typename

typedef is used to define types:
  1. For simplicity, clarity. such as,

    vector<list<int *>*> temp(10);
    

    Can be simplified to:

    typedef list<int *> listnum;
    typedef vector<listnum *> vectornum;
    vectornum temp(10);
    
  2. Define a pointer to the member:

    class A {
    public:
        virtual void sup() = 0;
    };
    typedef void (A::* pt)();
    void f(A *a)
    {
        pt ptemp = &A::sup;
    }
    
typedef typename
  1. template <typename var_name> class class_name; indicates that var_name is a type, which can replace any type when the template is instantiated, including not only built-in types (int, etc.), but also custom type classes. This is the form in the question. In other words, in template and template, typename and class have exactly the same meaning.

  2. typename var_name; means that the definition of var_name has not been given yet, this statement usually appears in the definition of the template, for example:

    template <class T>
    void f() {
    	typedef typename T::A TA;     // 声明 TA 的类型为 T::A
    	TA a5;                      // 声明 a5 的类型为 TA
    	typename T::A a6;             // 声明 a6 的类型为 T::A
    	TA * pta6;                     // 声明 pta6 的类型为 TA 的指针
    }
    

    Because T is a type that is only known when the template is instantiated , the compiler is more ignorant of T :: A. In order to inform the
    compiler that T :: A is a legal type, the typename statement can avoid the compiler from reporting an error.

Published 401 original articles · Liked 14 · Visits 100,000+

Guess you like

Origin blog.csdn.net/LU_ZHAO/article/details/105446458