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

被折叠的 条评论
为什么被折叠?



