次のように「オーバーロード」したいテンプレート構造があります。
#include <iostream>
template <typename T, typename U = int>
struct foo {
void operator()(T, U);
}
template <typename T, typename U = int>
void foo::operator()(T a, U b){
std::cout << "T, U ()\n";
}
template <typename T>
struct foo<T, int> {
void operator()(T);
}
template <typename T>
void foo<T, int>::operator()(T a){
std::cout << "T ()\n";
}
int main(int argc, char **argv){
foo<int> a;
foo<int, char> b;
a(1);
b(2, 'b');
return false;
}
しかし、コンパイルすると、次のエラーが発生します。
($ g++ test.cpp -o test)
test.cpp:11:6: error: 'template<class T, class U> struct foo' used without template parameters
test.cpp:11:30: error: 'void operator()(T, U)' must be a nonstatic member function
foo< T, int >::operator() の定義が完全に機能しているように見えるため、これは奇妙です。また、関数をインラインで次のように定義すると、次のようになります。
template <typename T, typename U = int>
struct foo {
void operator()(T a, U b){ std::cout << "T, U ()\n"; }
}
問題なく動作します。