나는이 같은이 "과부하"원하는 템플릿 구조를 가지고 :C++ 선언의 외부 템플릿 멤버 함수 정의는
($ 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
:
#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;
}
을하지만 다음과 같은 오류가 컴파일에
foo < T, int> :: operator()의 정의가 완벽하게 작동하기 때문에 이상합니다. 또한 다음과 같이 함수를 인라인으로 정의하면 :
template <typename T, typename U = int>
struct foo {
void operator()(T a, U b){ std::cout << "T, U()\n"; }
}
문제없이 작동합니다.