• 原理:C++为什么一般把模板实现放入头文件


    写在前面

    本文通过实例分析与讲解,解释了为什么C++一般将模板实现放在头文件中。这主要与C/C++的编译机制以及C++模板的实现原理相关,详情见正文。同时,本文给出了不将模板实现放在头文件中的解决方案。

    正文

    例子

    现有如下3个文件:

     1 // add.h
     2 template <typename T>
     3 T Add(const T &a, const T &b);
     4 
     5 // add.cpp
     6 #include "add.h"
     7 
     8 template <typename T>
     9 T Add(const T &a, const T &b)
    10 {
    11     return a + b;
    12 }
    13 
    14 // main.cpp
    15 #include "add.h"
    16 #include <iostream>
    17 
    18 int main()
    19 {
    20     int res = Add<int>(1, 2);
    21     std::cout << res << "\n";
    22     return 0;
    23 }
    示例代码

    现象

    使用 g++ -c add.cpp 编译生成 add.o ,使用 g++ -c main.cpp 编译生成 main.o ,这两步都没有问题。

    使用 g++ -o main.exe main.o add.o 生成 main.exe 时,报错 undefined reference to 'int Add(int const&, int const&)' 。

    当然,直接 g++ add.cpp main.cpp -o main.exe 肯定也会报错,这里把编译和链接分开是为了更好地展示与分析问题。​

    原因

    出现上述问题的原因是:

    (1)C/C++源文件是按编译单元(translation unit)分开、独立编译的。所谓translation unit,其实就是输入给编译器的source code,只不过该source code是经过预处理(pre-processed​,包括去掉注释、宏替换、头文件展开)的。在本例中,即便你使用 g++ add.cpp main.cpp -o main.exe ,编译器也是分别编译 add.cpp 和 main.cpp (注意是预处理后的)的。在编译 add.cpp 时,编译器根本感知不到 main.cpp 的存在,反之同理。

    (2) C++模板是通过实例化(instantiation)来实现多态(polymorphism)的。以函数模板为例,首先需要区分“函数模板”和“模板函数”。本例中,上面代码的第8~12行是函数模板,顾名思义,它就是一个模子,不是具体的函数,是不能运行的;当用具体的类型,如 int ,实例化模板参数 T 后,会生成函数模板的一个具体实例,称为模板函数,这是真正可以运行的函数。“函数模板”和“模板函数”的关系,可以类比“类”和“对象”的关系。以 int 为例,生成的实例/模板函数大概长这样(细节上肯定和编译器的实际实现有出入,但核心意思不会变)。

    1 int Add_int_int(const int &a, const int &b)
    2 {
    3     return a + b;
    4 }
    模板函数(示例)

    ​对于每一个用到的具体类型,编译器都会生成对应版本的实例,当函数调用时,会调用到该实例。如用到了 Add<int> ,就会生成 Add_int_int ,用到了 Add<double> ,就会生成 Add_double_double ,等等。本例中,当编译器编译到第20行,即 int res = Add<int>(1, 2); 一句时,编译器就会试图生成 int 版本的模板实例(即模板函数)。

    (3)编译器为模板生成实例的必要条件是:1. 知道模板的具体定义/实现;2. 知道模板参数对应的实际类型。

    分析

    下面把上面两节内容结合起来分析。

    (1)当编译 add.cpp 时,相当于编译

    1 template <typename T>
    2 T Add(const T &a, const T &b);
    3 
    4 template <typename T>
    5 T Add(const T &a, const T &b)
    6 {
    7     return a + b;
    8 }
    预处理后的add.cpp

    此时编译器虽然知道模板的具体定义,却不知道模板参数 T 的具体类型,因此不会生成任何的实例化代码。

    (2)当编译 main.cpp 时,相当于编译

     1 #include <iostream>
     2 
     3 template <typename T>
     4 T Add(const T &a, const T &b);
     5 
     6 int main()
     7 {
     8     int res = Add<int>(1, 2);
     9     std::cout << res << "\n";
    10     return 0;
    11 }
    预处理后的main.cpp

    当编译到 int res = Add<int>(1, 2); 时,编译器想要生成 int 版本的函数实例,但它找不到函数模板的具体定义(即 Add 的“函数体”),只好作罢。好在编译器看到了函数模板的声明,于是通过了编译,将寻找 int 版本函数实例的任务留给了链接器。​

    至此,编译 add.cpp 时,只知模板定义,不知模板类型参数,无法生成具体的函数定义;编译 main.cpp 时,只知模板类型参数,不知模板定义,同样无法生成具体的函数定义。​

    (3)没什么好说的,链接器在 add.o 和 main.o 中都没找到 int 版本的 Add 定义,直接报错。​

    解决方案

    方案一

    传统方法:把模板实现也放在头文件中。

     1 // add.h
     2 template <typename T>
     3 T Add(const T &a, const T &b)
     4 {
     5     return a + b;
     6 }
     7 
     8 // main.cpp
     9 #include "add.h"
    10 #include <iostream>
    11 
    12 int main()
    13 {
    14     int res = Add<int>(1, 2);
    15     std::cout << res << "\n";
    16     return 0;
    17 }
    解决方案一

    当编译 main.cpp 时,相当于编译​

     1 #include <iostream>
     2 
     3 template <typename T>
     4 T Add(const T &a, const T &b)
     5 {
     6     return a + b;
     7 }
     8 
     9 int main()
    10 {
    11     int res = Add<int>(1, 2);
    12     std::cout << res << "\n";
    13     return 0;
    14 }
    预处理后的main.cpp

    此时编译器既知道函数模板的定义,又知道具体的模板类型参数 int ,因此可以生成 int 版本的函数实例,不会出错。​

    这种方式的优缺点如下:

    • 优点:可以按需生成。假如我们在 main.cpp 中调用了 Add<double>(1.0, 2.0); ,编译器就会为我们生成 double 版本的函数实例。
    • 缺点:不得不把实现细节暴露给用户。

    方案二

    模板声明和定义分离的方案。​

     1 // add.h
     2 template <typename T>
     3 T Add(const T &a, const T &b);
     4 
     5 // add.cpp
     6 #include "add.h"
     7 
     8 template <typename T>
     9 T Add(const T &a, const T &b)
    10 {
    11     return a + b;
    12 }
    13 
    14 template int Add(const int &a, const int &b);
    15 
    16 // main.cpp
    17 #include "add.h"
    18 #include <iostream>
    19 
    20 int main()
    21 {
    22     int res = Add<int>(1, 2);
    23     std::cout << res << "\n";
    24     return 0;
    25 }
    解决方案二

    注意, template int Add(const int &a, const int &b); 是函数模板实例化(function template instantiation)[1], template 关键字不能省略,否则, int Add(const int &a, const int &b); 会被编译器当做普通函数的声明,从而在链接时又会报 undefined reference to 'int Add(int const&, int const&)' 错误。​

    对于这种写法,编译器在编译 add.cpp 时,既能看到函数模板的定义,又能看到具体的模板类型参数 int ,于是生成了 int 版本的函数实例,整个程序可以正常编译运行。​

    很显然,这种情况下编译器只生成了 int 版本的函数实例,所以,在 main.cpp 中使用 Add<double>(1.0, 2.0); 这样的代码肯定是不可以的。这种情况的优缺点可以辩证看待:​

    • 优点:1. 可以隐藏实现细节(我们可以把 add.cpp 做成.lib或.dll);2. 也可以限制只实例化特定的版本。​
    • 缺点:就是只能使用特定的几个版本,不能像方案一那样在编译 main.cpp 时根据具体的调用情况按需生成。​

    从这里也可以看出,模板实现不一定非得放在头文件中

    参考

    [1] Function template - cppreference.com
    [2] c++ - Why can templates only be implemented in the header file? - Stack Overflow

    写在后面

    本文从C/C++编译机制以及C++模板实现原理的角度,结合具体实例,讲解了为什么一般将模板实现放在头文件中。由于在下才疏学浅,能力有限,错误疏漏之处在所难免,恳请广大读者批评指正,您的批评是在下前进的不竭动力。

  • 相关阅读:
    2018-4-17-软件设计-白话依赖注入
    2018-2-13-wpf-PreviewTextInput-在鼠标输入获得-_u0003
    2018-5-23-为何-987654321_123456789-的值是-8.0000000729
    寄存器位写操作
    Linux多IP配置
    Kconfig和Makefile
    linux设置网卡速率
    Winmanager,NERDTree和MiniBufExplorer
    SuperTab
    ping
  • 原文地址:https://www.cnblogs.com/zpcdbky/p/16329886.html
Copyright © 2020-2023  润新知