wozu ist "typedef typename" ?



  • hallo

    ich wollt man fragen wozu ist

    typedef typename xxx yyy;
    

    ist das nicht dasselbe wie

    typedef xxx yyy;
    

    😕



  • typename wird meines wissens dann benutzt, wenn der compiler nicht auf anhieb den typ erkennen kann(oder so^^).
    //edit falsch^^

    //edit2 so hab mich eben nochmal ganz schnell schlau gemacht:

    nehmen wir mal folgenden fall:

    template<class T>
    struct a{
        typedef T b;
    };
    void test(a& b){
        typedef a::b parametertyp;//sinnlos, ich weis... 
    }
    

    das obere funktioniert, aber verädnern wir die funktion:

    template<class T>
    void test(T& b){
        typedef T::b parametertyp;
    }
    

    jetzt hat der compiler ein problem: hat jedes T wirklich einen typedef der b heist? das typename sagt dem compiler nur, dass er immer auf dem jeweils übergebenen typen achten soll.

    mit dem typename siehts dann soa us:

    template<class T>
    void test(T& b){
        typedef typename T::b parametertyp;
    }
    


  • ahja
    danke 🙂


Anmelden zum Antworten