开发者

Can I use C++ templates to generate Unicode/ANSI variants of a function, rather than using the preprocessor?

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 MyFuncti开发者_JAVA百科onName<wchar_t, std::wstring> MyFunctionNameW
typedef MyFunctionName<char, std::string> MyFunctionNameA

Is this possible?


Update: I misread the question, thinking you already had A and W functions written which you wanted to call through the same name. What you have is indeed a situation for which templates are designed, but I won't repeat the correct answer.

Simple overloading works, no need for templates or additional complexity. The Win32 API uses macros because C doesn't have overloading.

inline
void MyFunctionName(std::wstring const& value) { MyFunctionNameW(value); }
inline
void MyFunctionName(std::string const& value) { MyFUnctionNameA(value); }


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&);


yes its possible, this is basically what std::string and std::wstring do anyway as they are actually typedefs of std::basic_string<charT,traits,alloc>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜