Templatespezialisierung



  • template<int i>
    struct Foo{
    	template<int j>
    	operator Foo<j> ();
    };
    
    //template<>
    Foo<0>::operator Foo<2>(){
    	return Foo<2>();
    }
    

    Warum kompiliert das nicht und was muss man tun, damit es kompiliert?



  • Wenn eins nicht hilft, dann probiers mal mit zwei.

    template<int i>
    struct Foo{
        template<int j>
        operator Foo<j> ();
    };
    
    template<> // eins
    template<> // zwei
    Foo<0>::operator Foo<2>(){
        return Foo<2>();
    }
    


  • Cool danke funktioniert.



  • Vielleicht noch zur Erklärung, weshalb das so geht:

    template<> // Es handelt sich um eine Spezialisierung von Foo<>
    template<> // Es handelt sich um eine Spezialisierung von operator Foo<>
    Foo<0>::operator Foo<2>(){
        return Foo<2>();
    }
    

    Man würde auch schreiben

    template <typename T> // allgemein vector<T>
    void vector<T>::push_back(T const& x)
    { ... }
    
    template <typename T> // spezialisiert vector<T>
    template <typename... A> // allgemein für emplace_back(A...)
    // ^-- da sind auch zweimal template
    void vector<T>::emplace_back(A&&...a)
    { ... }
    

Anmelden zum Antworten