I want to define template variables without a class, but MSVC++ does not accept it, and Googling around it seems to be incorrect according to the C++ standard:
template<CharType> static CharType hexDigits[17];
template<> char hexDigits[17] = "0123456789ABCDEF";
template<> wchar_t hexDigits[17] = L"0123456789ABCDEF";
These specialized variables will then be used from within a (non-specialized) template function.
So I’m forced to write it like this:
template<typename CharType> class dummyclass {
static CharType hexDigits[17];
};
template<> char dummyclass<char>::hexDigits[17] = "0123456789ABCDEF";
template<> wchar_t dummyclass<wchar_t>::hexDigits[17] = L"0123456789ABCDEF";
Is there any way I can define these two variables without defining a dummy class?
Also, is there any good reason why the C++ standard does not allow the first piece of code? After all, template functions outside a class are permitted.
Note that this:
has two symbols with different types but the same name: this can’t possibly work, so the compiler would have to start mangling/decorating the names of variables like it already does for functions and classes.
In terms of implementing this cleanly, this looks like a trait to me … if you don’t mind getting a link error instead of a compile error, you can even skip the specialization and only declare the appropriate static members: