关于C++模板特化的简单例子

#include <iostream>
#include <concepts>
#include <vector>

// 参考 https://zhuanlan.zhihu.com/p/268600376

namespace test1 {

    // 类模板
    template<typename T1, typename T2>
    class A {
    public:
        A() {
            std::cout << "A<T1, T2>" << std::endl;
        }
    };

    // 类模板全特化
    template<>
    class A<char, int> {
    public:
        A() {
            std::cout << "A<char, int>" << std::endl;
        }
    };

    // 类模板偏特化
    template<typename T2>
    class A<char, T2> {
    public:
        A() {
            std::cout << "A<char, T2>" << std::endl;
        }
    };

    // 类模板
    template<typename T>
    class B {
    public:
        B() {
            std::cout << "B<T>" << std::endl;
        }
    };

    // 类模板全特化
    template<>
    class B<int> {
    public:
        B() {
            std::cout << "B<int>" << std::endl;
        }
    };

    // 类模板偏特化
    template<typename T>
    class B<std::vector<T> > {
    public:
        B() {
            std::cout << "B<vector<T> >" << std::endl;
        }
    };

}

namespace test2 {

    // 函数模板
    template<typename T1, typename T2>
    void function(const T1& t1, const T2& t2) {
        std::cout << "function<T1, T2>" << std::endl;
    }

    // 函数模板全特化
    template<>
    void function<float, int>(const float& t1, const int& t2) {
        std::cout << "function<float, int>" << std::endl;
    }

    // 函数模板不允许偏特化,与函数重载决策有关

    // 具体原因需要单独看看

    // template<typename T1, int>
    // void function<T1, int>(const T1& t1, const int& t2) {
    //     std::cout << "function<T1, int>" << std::endl;
    // }

    // 此重载函数将优先于相应全特化版本被选择
    // template<typename T>
    // void function(const T& t1, const int& t2) {
    //     std::cout << "function<T1, int>" << std::endl;
    // }

}

namespace test3 {

    // 解决方法1:functor
    template<typename T1, typename T2>
    class Function {
    public:
        void operator()(const T1& t1, const T2& t2) {
            std::cout << "Function<T1, T2>" << std::endl;
        }
    };

    template<>
    class Function<int, char> {
    public:
        void operator()(const int& t1, const char& t2) {
            std::cout << "Function<int, char>" << std::endl;
        }
    };

    template<typename T>
    class Function<T, char> {
    public:
        void operator()(const T& t1, const char& t2) {
            std::cout << "Function<T, char>" << std::endl;
        }
    };

}

namespace test4 {

    // 解决方法2:标签转发
    struct NormalTag {};
    struct IntTag {};

    template<typename T> struct TagDispatch { using Tag = NormalTag; };
    template<> struct TagDispatch<int> { using Tag = IntTag; };

    template<typename T1, typename T2>
    inline void function_impl(const T1& t1, const T2& t2, NormalTag) {
        std::cout << "normal tag" << std::endl;
    }

    template<typename T1, typename T2>
    inline void function_impl(const T1& t1, const T2& t2, IntTag) {
        std::cout << "int tag" << std::endl;
    }

    template<typename T1, typename T2>
    void function(const T1& t1, const T2& t2) {
        function_impl(t1, t2, typename TagDispatch<T2>::Tag {});

        // 等价于:
        // typename TagDispatch<T2>::Tag tag;
        // function_impl(t1, t2, tag);
    }

}

namespace test5 {

    // 解决方法3:使用concept
    template<typename T1, typename T2>
    void function(const T1& t1, const T2& t2) {
        std::cout << "default" << std::endl;
    }

    template<typename T1, typename T2>
    requires std::integral<T2>
    void function(const T1& t1, const T2& t2) {
        std::cout << "use requires" << std::endl;
    }

    // 关于concept需要单独看看

}

int main() {
    test1::A<int, int> a1;
    test1::A<char, int> a2;
    test1::A<char, char> a3;

    test1::B<char> b1;
    test1::B<int> b2;
    test1::B<std::vector<int> > b3;

    test2::function(10, 1.0);
    test2::function(1.f, 10);

    test3::Function<int, int>()(10, 10);
    test3::Function<int, char>()(10, '1');
    test3::Function<float, char>()(1.f, '1');

    test4::function(1, 1.f);
    test4::function(1, 2);

    test5::function(1, 1.f);
    test5::function(1, 111);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值