6

Ich versuche, eine Templat-Memberfunktion einer untemplated Klasse nur teilweise zu spezialisieren:Teil Vorlage Spezialisierung der Member-Funktion: „Prototyp stimmt nicht überein“

#include <iostream> 

template<class T> 
class Foo {}; 

struct Bar { 

    template<class T> 
    int fct(T); 

}; 

template<class FloatT> 
int Bar::fct(Foo<FloatT>) {} 


int main() { 
    Bar bar; 
    Foo<float> arg; 
    std::cout << bar.fct(arg); 
} 

ich die folgende Fehlermeldung erhalten:

c.cc:14: error: prototype for ‘int Bar::fct(Foo<FloatT>)’ does not match any in class ‘Bar’ 
c.cc:9: error: candidate is: template<class T> int Bar::fct(T) 

Wie kann ich den Compilerfehler beheben?

Antwort

9

Teilspezialisierung von Funktionen (Mitglied oder nicht) ist nicht erlaubt.

Verwenden Überlastung:

struct Bar { 

    template<class T> 
    int fct(T data); 

    template<class T> //this is overload, not [partial] specialization 
    int fct(Foo<T> data); 

};