作为一个模板,编译器不会执行任何自动类型转换。当编译器无法推断函数模板的模板参数时应当在尖括号中告诉编译器要使用哪种类型作为模板参数。
编译器无法知道你想要模板参数具有第一个函数参数的类型,或第二个函数参数的类型,或者有时是第一个,有时是第二个。相反,编译器要求你明确地写出你的意思。在这种情况下,你可以通过将所需的类型放在尖括号中告诉编译器要使用哪种类型作为模板参数。
示例一:
#include <iostream>
#include <vector>
template<class T>
T my_max(T a, T b){
return (a >= b? a:b);
}
int main() {
int a = 100;
long b = 200;
// auto max = my_max(a, b); // error: no matching function for call to 'my_max(int&, long int&)'
auto max1 = my_max<int>(a, b);
auto max2 = my_max<long>(a, b);
std::cout << "max1 = " << max1 << ", max2 = " << max2 << std::endl;
}
示例二:
#include <iostream>
#include <cstdlib>
/* Compute the greatest common divisor of two integers, using Euclid's algorithm. */
template<class T>
T gcd(T n, T m){
n = std::abs(n);
while(m != 0){
T tmp{n % m};
n = m;
m = tmp;
}
return n;
}
int main() {
int a = 100;
long b = 200;
int c = 15;
auto val1 = gcd<int>(a, b);
auto val2 = gcd<long>(a, b);
auto val3 = gcd(a, c);
std::cout << "val1 = " << val1 << ", val2 = " << val2 << ", val3 = " << val3 << std::endl; // val1 = 100, val2 = 100, val3 = 5
}
示例三:
#include <iostream>
#include <vector>
template<class T>
class Rational{
public:
// Delegating Constructor
Rational(): Rational(0){
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
Rational(T num): numerator_(num), denominator_(1){
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
Rational(T num, T den): numerator_(num), denominator_(den){
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
virtual ~Rational(){
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
T numerator()const{
return numerator_;
}
T denominator()const{
return denominator_;
}
private:
T numerator_;
T denominator_;
};
template<class T>
bool operator==(const Rational<T>& lhs, const Rational<T>& rhs){
if(lhs.numerator() == rhs.numerator() && lhs.denominator() == rhs.denominator()){
return true;
}
else{
return false;
}
}
#if 0 // 如果类模板的模板参数与rhs不同,可选地将rhs也改为模板的形式
template<class T, class U>
bool operator==(const Rational<T>& lhs, U rhs){
if(lhs.numerator() == rhs && lhs.denominator() == 1){
return true;
}
else{
return false;
}
}
#endif
template<class T>
bool operator==(const Rational<T>& lhs, T rhs){
if(lhs.numerator() == rhs && lhs.denominator() == 1){
return true;
}
else{
return false;
}
}
template<class T>
bool operator==(T lhs, const Rational<T>& rhs){
if(rhs.numerator() == lhs && rhs.denominator() == 1){
return true;
}
else{
return false;
}
}
template<class T>
bool operator!=(const Rational<T>& lhs, const Rational<T>& rhs){
return !(operator==(lhs, rhs));
}
template<class T>
bool operator!=(const Rational<T>& lhs, T rhs){
return !(operator==(lhs, rhs));
}
template<class T>
bool operator!=(T lhs, const Rational<T>& rhs){
return !(operator==(lhs, rhs));
}
int main() {
Rational<float> r1;
Rational<int> r2;
std::cout << (r1 == (float)0) << std::endl;
std::cout << (r2 == 0) << std::endl;
std::cout << (r1 == r1) << std::endl;
std::cout << (r1 != r1) << std::endl;
std::cout << r1.numerator() << "/" << r1.denominator() << std::endl;
}
输出:
Rational<T>::Rational(T) [with T = float]
Rational<T>::Rational() [with T = float]
Rational<T>::Rational(T) [with T = int]
Rational<T>::Rational() [with T = int]
1
1
1
0
0/1
Rational<T>::~Rational() [with T = int]
Rational<T>::~Rational() [with T = float]
Reference
Exploring C++ 11, 2nd Edition.