2009-06-02 72 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))不同。