J
johnbrown105
Is it possible to force the compiler to use a generic template rather
than a
matching specialisation?
Consider the following:
////////////////////////////////////////// template.cpp starts here
#include <iostream>
#include <ostream>
template<class T>
void f(T t)
{
std::cout << "template<T> void f(T t) called: t == " << t
<< std::endl;
}
template<> void f<int>(int i)
{
std::cout << "specialisation void f<int>(int i) called: i == "
<< i << std::endl;
}
void f(int j){
std::cout << "overloaded void f(int j) called: j == "
<< j << std::endl;
}
int main()
{
f(1); // simple function matches before template
f<int>(2); // specialisation
f<>(3); // no need to spell out <int>, because it *must* be int
f('4'); // template matches - f<char>(char)
return 0;
}
////////////////////////////////////////// template.cpp ends here
Is there a way to call f with an int parameter so that the generic
f<T>
is called instead of the f<int> specialisation?
I don't want to do this, and I cannot think of a reason for doing it.
I'm just wondering.
than a
matching specialisation?
Consider the following:
////////////////////////////////////////// template.cpp starts here
#include <iostream>
#include <ostream>
template<class T>
void f(T t)
{
std::cout << "template<T> void f(T t) called: t == " << t
<< std::endl;
}
template<> void f<int>(int i)
{
std::cout << "specialisation void f<int>(int i) called: i == "
<< i << std::endl;
}
void f(int j){
std::cout << "overloaded void f(int j) called: j == "
<< j << std::endl;
}
int main()
{
f(1); // simple function matches before template
f<int>(2); // specialisation
f<>(3); // no need to spell out <int>, because it *must* be int
f('4'); // template matches - f<char>(char)
return 0;
}
////////////////////////////////////////// template.cpp ends here
Is there a way to call f with an int parameter so that the generic
f<T>
is called instead of the f<int> specialisation?
I don't want to do this, and I cannot think of a reason for doing it.
I'm just wondering.