C++成员模版函数编译分离

 .h

class TestTemplateFunc
{
public:
	template<typename T>
	inline void setParam(const T& t);
};

.cpp

#include "TestTemplateFunc.h"

#include<iostream>

template<typename T>
 void TestTemplateFunc::setParam(const T& t) {
	std::cout << " setParam T" << std::endl;
}

template<>
 void TestTemplateFunc::setParam<double>(const double& t) {
	std::cout << "set Param double" << std::endl;
}

// 显式实例化模板
template void TestTemplateFunc::setParam<int>(const int& t); // 显式实例化 double 类型

main.cpp

int main() {
    TestTemplateFunc func;
    func.setParam<int>(2);            //通用模版
    func.setParam<double>(4.23);      //模版特化

    return 0;
}

result:

tips:

向这种成员函数模版,可以用函数重载去替代实现。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值