Hi,
If I do this:
void
func(const int& i) {...}
template<class T>
func(const T& i) {...}
Is this equivalent to, or different to, the following?
template<class T>
func(const T& i) {...}
template<>
void
func<int>(const int& i) {...}
Different, but you have to stress it to make the difference show up.
Consider this:
#include <iostream>
using namespace std;
void func1(const int& i) { cout << "func1(const int &)" << endl;}
template<class T>
void func1(const T& i) { cout << "func1(const T &) [template]" << endl;}
// Is this equivalent to, or different to, the following?
template<class T>
void func2(const T& i) { cout << "func2(const T &) [template]" << endl;}
template<>
void func2<int>(const int& i) { cout << "func2(const int&) [template]" <<
endl;}
int main()
{
cout << "Using ints:" << endl;
func1(5);
func2(5);
cout << endl;
cout << "Using longs:" << endl;
func1(5L);
func2(5L);
return 0;
}
Output:
Using ints:
func1(const int &)
func2(const int&) [template]
Using longs:
func1(const T &) [template]
func2(const T &) [template]
The difference is in the overload resolution rules. Using ints, the best
match is the completely-ordinary non-template.
Using longs, an exact match of a template specialization beats using a
non-template where a conversion has to happen.
-leor