模板string怎么初始化静态变量
template<class STR>
class str_c
{
typedef STR string;
static string _sBody;
};
template<class STR>
STR str_c<STR>::_sBody="name";但是这里有一个问题,如果STR是宽字符的时候
name就不能给_sBody,必须要变成L"name"才能赋值
有什么办法解决吗?
[解决办法]
简单点可以对_sBody做一个特化,例如:
template<>
const wchar_t* str_c< const wchar_t* >::_sBody = L"name";
template <typename _CharT>
struct str_c;
template <>
struct str_c<char>
{
typedef string string_type;
static string_type s_body;
};
str_c<char>::string_type str_c<char>::s_body = "中国";
template <>
struct str_c<wchar_t>
{
typedef wstring string_type;
static string_type s_body;
};
str_c<wchar_t>::string_type str_c<wchar_t>::s_body = L"你好";