次の関数テンプレートの2番目の角かっこ<>の理由は何ですか。
template<> void doh::operator()<>(int i)
これはSOの質問で出てきて、後に角かっこが欠落していることが示唆されましたが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
これはこのシナリオのどこに当てはまりますか?:
template<> void doh::operator()<>(bool b) {}
動作しているように見え、警告/エラーを表示しないサンプルコード(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)