什么是第二个括号<>下面的函数模板的原因:函数模板特格式
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)
上面的双括号语法很奇怪。通常我看过operator(bool b),但operator()(bool b)如何工作?第一个空()的用法是什么? – Jimm 2012-01-05 20:30:51
@Jimm方法名是`operator()`,它需要一个参数`bool b` ...这是函数调用操作符。请参阅我的代码示例中的`main`,`d(b)` – stefanB 2012-01-05 22:00:44