HiroIshida
HiroIshida

Reputation: 1603

template specialization causes a undefined reference error

I tried a template specialization like bellow.

#include<iostream>
using namespace std;

template<class T1, class T2>
T1 something(T2 a);

template<class T2>
double something(T2 a){
    double b;
    return b;
}

int main(){
    something<double, double>(0.0);
}

However, this gives me a compilatoin error:

In function `main':
test.cpp:(.text+0x9): undefined reference to `double something<double, double>(double)'

Could you tell me how to fix it?

Upvotes: 3

Views: 403

Answers (1)

songyuanyao
songyuanyao

Reputation: 172864

This is not template specialization, but function template overloading.

The 1st overloading has two template parameters, the 2nd one has only one; when you call it with two specified template arguments like something<double, double>(0.0);, the 1st one will be selected in overload resolution; but it's not defined then leads to link error.


BTW: Function templates can only be full specialized, they can't be partial specialized. And in most cases function template overloading would do the work well.

Upvotes: 5

Related Questions