6

我想部分專門的untemplated類的模板成員函數:模板偏特:「原型不符」

#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); 
} 

,我發現了以下錯誤:

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) 

我該如何解決編譯器錯誤?

回答

9

功能(成員或其他)的部分專業化是不允許的。

使用過載:

struct Bar { 

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

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

};