对于模板,编译器不会执行任何自动类型转换

12 篇文章 0 订阅
文章讲述了C++中如何在编译器无法推断模板参数类型时,通过尖括号明确指定类型,以及展示了如何在类模板中实现运算符重载。通过示例展示了模板参数的使用和构造函数的模板化过程。
摘要由CSDN通过智能技术生成

作为一个模板,编译器不会执行任何自动类型转换。当编译器无法推断函数模板的模板参数时应当在尖括号中告诉编译器要使用哪种类型作为模板参数。

编译器无法知道你想要模板参数具有第一个函数参数的类型,或第二个函数参数的类型,或者有时是第一个,有时是第二个。相反,编译器要求你明确地写出你的意思。在这种情况下,你可以通过将所需的类型放在尖括号中告诉编译器要使用哪种类型作为模板参数。

示例一:

#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.

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值