2009-06-02 73 views
30

什麼是第二個括號<>下面的函數模板的原因:函數模板特格式

template<> void doh::operator()<>(int i) 

SO question想出了它被認爲有括號缺失operator()後,但是我可以找不到解釋。

我明白其中的含義,如果它是形式的專業化類型(完全專業化):

template< typename A > struct AA {}; 
template<> struct AA<int> {};   // hope this is correct, specialize for int 

然而,對於函數模板:

template< typename A > void f(A); 
template< typename A > void f(A*); // overload of the above for pointers 
template<> void f<int>(int);   // full specialization for int 

哪裏這個適應這個scenarion?:

template<> void doh::operator()<>(bool b) {} 

示例代碼,似乎工作,並沒有給任何華rnings /錯誤(用gcc 3.3.3):

#include <iostream> 
using namespace std; 

struct doh 
{ 
    void operator()(bool b) 
    { 
     cout << "operator()(bool b)" << endl; 
    } 

    template< typename T > void operator()(T t) 
    { 
     cout << "template <typename T> void operator()(T t)" << endl; 
    } 
}; 
// note can't specialize inline, have to declare outside of the class body 
template<> void doh::operator()(int i) 
{ 
    cout << "template <> void operator()(int i)" << endl; 
} 
template<> void doh::operator()(bool b) 
{ 
    cout << "template <> void operator()(bool b)" << endl; 
} 

int main() 
{ 
    doh d; 
    int i; 
    bool b; 
    d(b); 
    d(i); 
} 

輸出:

operator()(bool b) 
template <> void operator()(int i) 
+0

上面的雙括號語法很奇怪。通常我看過operator(bool b),但operator()(bool b)如何工作?第一個空()的用法是什麼? – Jimm 2012-01-05 20:30:51

+1

@Jimm方法名是`operator()`,它需要一個參數`bool b` ...這是函數調用操作符。請參閱我的代碼示例中的`main`,`d(b)` – stefanB 2012-01-05 22:00:44

回答

31

我看着它,並發現它是由14.5.2/2中指定:

A local class shall not have member templates. Access control rules (clause 11) apply to member template names. A destructor shall not be a member template. A normal (non-template) member function with a given name and type and a member function template of the same name, which could be used to generate a specialization of the same type, can both be declared in a class. When both exist, a use of that name and type refers to the non-template member unless an explicit template argument list is supplied.

,它提供了一個例子:

template <class T> struct A { 
    void f(int); 
    template <class T2> void f(T2); 
}; 

template <> void A<int>::f(int) { } // non-template member 
template <> template <> void A<int>::f<>(int) { } // template member 

int main() 
{ 
    A<char> ac; 
    ac.f(1); //non-template 
    ac.f(’c’); //template 
    ac.f<>(1); //template 
} 

注意s標準術語,specialization引用您使用顯式專門化編寫的函數和使用實例化生成的函數,在這種情況下,我們必須處理生成的專用化。 specialization不僅僅是指您使用顯式專門化模板創建的函數,通常只使用它。

結論:GCC錯了。科莫,與我也測試代碼,得到它的權利,併發出診斷:

"ComeauTest.c" , line 16: error: "void doh::operator()(bool)" is not an entity that can be explicitly specialized template<> void doh::operator()(bool i)

注意,它不是抱怨模板int(僅適用於bool)的專業化,因爲它不」 t指同名類型:專業化的功能類型爲void(int),與非模板成員函數的功能類型(void(bool))不同。