템플릿 인수 형식이 동일한 경우 두 개의 템플릿 인수 함수를 특수화하려고합니다. 나는 그에게 다음과 같은 방법으로 수행템플릿 함수 특수화 : 링커 오류
#include <iostream>
#include <type_traits>
using namespace std;
template<typename U, typename T>
int fun(U& u, T t);
template<>
inline
int fun(int& u, float t)
{
cout << "int, float" << endl;
return 0;
}
template<typename U, typename T>
inline
int fun(U& u, typename std::enable_if<std::is_same<U, T>::value ,T>::type t)
{
cout << "U == T" << endl;
return 0;
}
int main()
{
int a;
float b1, b2;
fun(a, b1);
fun(b1, b2);
return 0;
}
이 코드는 잘 컴파일 (GCC 4.8.2)하지만, 링커는 U
및 T
가 동일한 유형의 모든 fun
호출에 정의되지 않은 refernces을 제공합니다. 왜 작동하지 않습니까?
링커 출력 :
g++ -std=c++11 test.cpp
/tmp/cc7HWboz.o: In function `main':
test.cpp:(.text+0x66): undefined reference to `int fun<float, float>(float&, float)'
collect2: error: ld returned 1 exit status
'T'와 'U'가 다른 경우 링커 오류가 없습니까? –
@KerrekSB 정답, 구체적인 예를 들어 편집 된 게시물 참조 – user2052436