c++ 模板函数 定义在 头文件 和源文件的区别

模板函数在声明后被调用时进行编译。

当编译器遇到函数模板的定义时,它并不立即生成代码。只有在模板被实例化,即生成特定版本的代码时,编译器才会进行编译。这意味着,只有在模板被实际使用时,编译器才会根据提供的参数类型生成相应的函数代码。这种延迟生成代码的方式,使得模板的使用更加灵活,同时也提高了编译效率,因为只有在实际需要的部分才会被编译进最终的执行文件中。

因此,当模板被调用并且参数类型确定后,生成具体的函数代码。如果多次调用使用相同的参数类型,编译器则不会再次编译,而是复用已经生成的函数。

模板函数应该定义在头文件中,而不是源文件中。

模板函数与普通函数的一个关键区别在于,模板函数只有在真正调用时才会根据实际参数编译,生成相应的二进制机器指令。如果模板函数的定义和实现被分开写在头文件和源文件中,当其他源文件包含这个头文件时,编译器会在编译时遇到模板函数的声明,但由于此时模板函数尚未被编译,因此没有相应的定义可供链接,只有模板声明。这会导致链接器在寻找模板函数的定义(实现)时失败,因为模板函数的实现尚未生成。

为了解决这个问题,模板函数的定义应该直接放在头文件中。这样,当其他源文件包含这个头文件时,编译器可以在编译阶段直接处理模板函数的声明和定义,而无需等到链接阶段。这样做可以确保模板函数的使用和实例化在编译时完成,避免了链接时的错误。

案例1:在include.h头文件中声明模板函数 ,test.cpp内实现模板函数 完整代码

//include.h

#include<iostream>
using namespace std;

template<typename T>
void fun1(T args);
//fun2.cpp

#include "include.h"

void fun2()
{
        double d = 15.123;
        fun1(d);
}

//test.cpp

#include "include.h"


template<typename T>
void fun1(T args)
{
        T sum = 0;
        sum = args + args;
        std::cout<<"sum:"<<sum<<std::endl;
}

extern void fun2();

int main()
{
        int i=10;
        fun1(i);

        double d= 12.15;
        fun1(d);

        fun2();
        return 0;
}

 g++ test.cpp fun2.cpp -o test -std=c++11

打印结果:

[root@zyysp2 template]# ./test 
sum:20
sum:24.3
sum:30.246

 这里fun2.cpp源文件内包含了 模板函数的声明 并没有模版函数的实现 为什么正常运行,这里就是上面讲到的,“如果多次调用使用相同的参数类型,编译器则不会再次编译,而是复用已经生成的函数。”  double类型的模板函数已经编译过了

如果把test.cpp内的下面这段注释:

//      double d= 12.15;
//      fun1(d);

则编译报错: 没有定义fun1模板函数

[root@zyysp2 template]# g++ test.cpp fun2.cpp -o test -std=c++11
/tmp/cc7qeFqE.o: In function `fun2()':
fun2.cpp:(.text+0x24): undefined reference to `void fun1<double>(double)'
collect2: error: ld returned 1 exit status
[root@zyysp2 template]# 

如果将模板函数声明 定义都写在include.h文件内 那一切正常了:

完整源码

[root@zyysp2 template]# cat include.h 
#include<iostream>
using namespace std;

template<typename T>
void fun1(T args)
{
        T sum = 0;
        sum = args + args;
        std::cout<<"sum:"<<sum<<std::endl;
}


[root@zyysp2 template]# cat fun2.cpp
#include "include.h"

void fun2()
{
        double d = 15.123;
        fun1(d);
}


[root@zyysp2 template]# cat test.cpp
#include "include.h"

/*
template<typename T>
void fun1(T args)
{
        T sum = 0;
        sum = args + args;
        std::cout<<"sum:"<<sum<<std::endl;
}
*/

extern void fun2();

int main()
{
        int i=10;
        fun1(i);

//      double d= 12.15;
//      fun1(d);

        fun2();
        return 0;
}

 g++ test.cpp fun2.cpp -o test -std=c++11

[root@zyysp2 template]# ./test 
sum:20
sum:30.246

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值