Monday, 15 June 2015

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


We've got a bunch of inheritance code that does not support Unicode, so in our code we use a transitional pattern To move the function to the .inl file, change char to CHAR_TYPE , and then wrap it like this:

  #define CHAR_TYPE wchar_t #define STRING_TYPE std :: wstring #define MyFunctionName MyFunctionNameW #include "MyFunction.inl" #undef CHAR_TYPE #define CHAR_TYPE Characters #undef STRING_TYPE #define STRING_TYPE std :: string #undef myFunctionName #define MyFunctionName MyFunctionNameA #include "MyFunction.inl"   

... where MyFunction.inl then defines MyFunctionName , using the macro to generate both 'A' Version and a 'W' version.

This is icky, but unfortunately it is necessary unless we get all the code changed to support Unicode.

Is there any alternative way I can do this with templates? I think something good would be like the following:

  typedef MyFunctionName & lt; Wchar_t, std :: wstring & gt; MyFunctionNameW typedef MyFunctionName & lt; Char, std :: string & gt; My function name << / code> 

Is this possible?

Roger Pattay is absolutely correct about the interface, do not bother you with A and W suffix needed. However, it still leaves the problem of implementation as you thought, the templates are the right solution and since you do not need different names, you can leave type-pff. You now have

templates & lt; Typename TSTRING & gt; Zero MyFunctionName (TSTRING const & amp;);


No comments:

Post a Comment