在模板类中使用字符串作为无类型模板参数

本文介绍如何正确使用字符串作为模板参数,包括避免使用const char*类型的注意事项、实例化模板类时的extern声明方法以及正确的实现位置。

template<class Item_type, const char xml_root_name[]> class CConfigItemList{ public: CConfigItemList(){ m_xml_root_name = xml_root_name; } private: string m_xml_root_name; } extern const char hostport_root_name[]; typedef CConfigItemList<string,hostport_root_name> HostPortList_t;

使用字符串做为无类型模板参数时要注意以下几点:

1、 template<class Item_type, const char xml_root_name[]>,中不要使用const char* 等类型。

2、在将模板类实例化时,定义的参数要使用extern 声明。extern const char hostport_root_name[];

3、如果该类是写在“.h”文件里的,不要直接extern const char hostport_root_name[] = “hostport”;,而是在头文件里只声明,在CPP中写上实现,const char hostport_root_name[] = “hostport”;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值