Here is my simplestring class -- a template class which is supposed to deal with both char string and wchar_t string. I want to define a default_separator for both char string and wchar_t string:
When _Thar is char, default_separator is "\n" and when _Thar is wchar_t, default_separator is L"\n"
I define a MACRO DEFAULT_SEPARATOR as "\n", and I want to assign the value for both of the specilized member simplestring::default_separator and simplestring<wchar_t>::default_separator,
template<typename _TChar>
struct simplestring
{
#define DEFAULT_SEPARATOR "\n"
static const _TChar* default_separator;
void show_tstring()
{
throw "no implement";
}
};
template<>
const char* simplestring<char>::default_separator = DEFAULT_SEPARATOR;
template<>
const wchar_t* simplestring<wchar_t>::default_separator = L##DEFAULT_SEPARATOR; //doesn't compile
void simplestring<char>::show_tstring()
{
cout << "char string\n";
cout << default_separator << endl;
}
void simplestring<wchar_t>::show_tstring()
{
cout << "wchar_t string\n";
wcout << default_separator << endl;
}
static void test()
{
simplestring<char> s;
s.show_tstring();
simplestring<int> s2;
s2.show_tstring();
}
The key line L##DEFAULT_SEPARATOR
doesn't compile with error
Error C2014 preprocessor command must start as first nonwhite space ForTest