We’ve got a bunch of legacy code that doesn’t support Unicode, so a transitional pattern we use in our code is to move the function to a .inl file, change char to CHAR_TYPE, and then wrap it up like this:
#define CHAR_TYPE wchar_t
#define STRING_TYPE std::wstring
#define MyFunctionName MyFunctionNameW
#include "MyFunction.inl"
#undef CHAR_TYPE
#define CHAR_TYPE char
#undef STRING_TYPE
#define STRING_TYPE std::string
#undef MyFunctionName
#define MyFunctionName MyFunctionNameA
#include "MyFunction.inl"
…where MyFunction.inl then defines MyFunctionName, using the macros to generate both an ‘A’ version and a ‘W’ version.
This is icky, but it’s unfortunately necessary until we get all of our code converted to support Unicode.
Is there an alternative way I could do this with templates? I’m thinking that something like the following would be nice:
typedef MyFunctionName<wchar_t, std::wstring> MyFunctionNameW
typedef MyFunctionName<char, std::string> MyFunctionNameA
Is this possible?
Roger Pate is entire correct about the interface. You shouldn’t bother with A and W suffixes. However, this still leaves the problem of implementation. As you supected, templates are the correct solution. And since you don’t need the different names, you can leave out the typedefs. You would just have
template <typename TSTRING> void MyFunctionName (TSTRING const&);