• C++类模板实例化与专门化


    12.8 C++类模板实例化与专门化

    12.8.1 隐式实例化

    编译器只有在生成模板对象的时候才会生成模板类的实例化类定义,然后根据实例化类生成对象。

    12.8.2 显式实例化

    12.8.2.1 定义

    使用关键字template并指定类型的语句,编译器就会根据这个表达式生成类定义。

    12.8.2.2 格式
    template class ArrayTP<string,100>;
    
    • 1
    12.8.2.3 注意事项

    要求类模板定义和显式实例化语句在同一命名空间中。

    12.8.3 显式专门化

    12.8.3.1 定义

    在通用的模板类下,一些类型满足模板类的所有功能要求;但是总有一些特殊类,对于一些功能可能需要特殊化处理;此时就需要用到显式专门化。
    比如>符号可以比较两个数据的大小,但是当类型为char *并且指向字符串时,显然>不能比较两字符串的大小,需要使用strcmp(),这就需要更改模板类的相关方法。

    12.8.3.2 格式
    template <> class Classname<specialized-type-name>{...};
    
    • 1
    12.8.3.3 优先级

    在通用模板和专门化模板都符合实例化要求时,编译器优先选择专门化模板。

    12.8.3.4 举例
    template <> class SortedArray<const char char *>
    {
        ...// details omitted
    };
    SortedArray<int> scores; // use general definition
    SortedArray<const char *> dates; // use specialized definition
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    12.8.4 部分专门化

    12.8.4.1 定义

    部分专门化可以为其中一个类型参数提供特定类型

    12.8.4.2 格式
    template <class T1> class Pair<T1, int> {...};
    
    • 1
    12.8.4.3 注意事项

    <>中包含的是没有被专门化的;如果<>为空则得到一个显式专门化。

    12.8.4.4 优先级

    编译器会在符合要求的所有模板中选择专门化程度最高的。

    12.8.4.5 举例
    // general template
    template <class T1, class T2> class Pair {...};
    // specialization with T2 set to int
    template <class T1> class Pair<T1, int> {...};
    // specialization with T1 and T2 set to int
    template <> class Pair<int, int> {...};
    Pair<double, double> p1; // use general Pair template
    Pair<double, int> p2; // use Pair partial specialization
    Pair<int, int> p3; // use Pair explicit specialization
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    12.8.5 指针部分专门化

    template<class T> // general version
    class Feeb { ... };
    template<class T*> // pointer partial specialization
    class Feeb { ... }; // modified code
    Feeb<char> fb1; // use general Feeb template, T is char
    Feeb<char *> fb2; // use Feeb T* specialization, T is char
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    12.8.6 允许使用未专门化的参数专门化其他参数

    // general template
    template <class T1, class T2, class T3> class Trio{...};
    // specialization with T3 set to T2
    template <class T1, class T2> class Trio<T1, T2, T2> {...};
    // specialization with T3 and T2 set to T1*
    template <class T1> class Trio<T1, T1*, T1*> {...};
    Trio<int, short, char *> t1; // use general template
    Trio<int, short> t2; // use Trio
    Trio<char, char *, char *> t3; use Trio<T1, T1*, T1*>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

  • 相关阅读:
    论文阅读【4】Product-based Neural Networks for User Response Prediction
    聊聊Spring的Aware接口
    利用小批量训练的方法在子图中进行消息传递
    springboot:集成Kaptcha实现图片验证码
    Apollo 应用与源码分析:Monitor监控-软件监控-进程存活监控-process_monitor
    <el-input-number>显示两位数字;如果是一位数字的话前面补0
    基于stm32单片机智能交通灯设计Proteus仿真
    神经网络模型画图工具,神经网络模型图怎么画
    思科dhcp服务器动态获取ip地址
    【HarmonyOS NEXT】鸿蒙customScan (自定义界面扫码)
  • 原文地址:https://blog.csdn.net/weixin_44410704/article/details/127983779