Template spezialisierung



  • Hi,

    kann ich ein Funktionstemplate mit einer Template-Klasse spezialisieren? Irgendwie krieg ich das nicht gebacken 😕

    template<int Size, class Type> class TypeVector;
    
    class x {
      ...
      template<class Type> char* DoFormat(Type const&);
      ...
    };
    
    // funktioniert ...
    template<class Type> char*
    x::DoFormat<Type>(Type const &d) {
      // do something
    }
    
    // meldet: cannot generate template specialization from TypeVector<Size, Type>
    template<> char*
    x::DoFormat<TypeVector<> >(TypeVector<> const &d) {
      // do something specific to vectors
    }
    

    Geht das grundsaetzlich nicht, oder mach ich da was falsch???



  • bei TypeVector fehlen doch die template parameter...



  • ich hatte auch schon mal vesucht die Funktion mit

    template<int Size, class Type> char*
    x::DoFormat<TypeVector<Size, Type> >(TypeVector<Size, Type> const &d) {
    
    }
    

    zu spezialisieren. Da bekomme ich aber dann die Fehlermeldung, dass die Funktion kein Member von x ist ...



  • Hallo,
    du kannst ein Membertemplate entweder explizit spezialisieren

    template<> 
    char* 
    x::DoFormat(TypeVector<5, int> const &d) { 
      // do something specific to vectors 
    }
    

    oder aber überladen:

    class x { 
      template<class Type> char* DoFormat(Type const&); 
      template <int i, class Type> char* DoFormat(TypeVector<i, Type> const&);
    }; 
    
    template<class Type> char* 
    x::DoFormat(Type const &d) { 
      // do something 
    } 
    
    template<int i, class T> 
    char* 
    x::DoFormat(TypeVector<i, T> const &d) { 
      // do something specific to vectors 
    }
    

    Partiell spezialisieren kannst du ein Membertemplate aber nicht.



  • Ok, danke. Ich hatte sowas schon befuerchtet ...


Anmelden zum Antworten