Standard macro for promoting a string literal in wchar_t in VC ++

Is there a standard macro for converting a string literal char that is replaced by a preprocessor macro with a wchar_t string using Visual Studio 2005?

eg. it works:

wchar_t* test = L"Hello World"; 

but this is not so:

 #define HELLOWORLD "Hello World" wchar_t* test = L(HELLOWORLD); 

I use a header file containing many internationalized strings with several different projects on different platforms, so I donโ€™t want to change the header itself and not add _T (), as it depends on the platform. Therefore, I want the Unicode conversion to be in my source, and not in the header. I know that I can write my own substitution macro, as shown here , but I wonder if there is a standard method in VS?

This may seem like a very common scenario for those writing internationalized code, but I cannot find the predefined macro that ships with VS2005.

+3
source share
2 answers

No, there is no "standard" macro for this. The standard is to prefix wide lines with L and omit the prefix from narrow lines.

The only reason this macro should even exist is because you target platforms that do not support Unicode in different ways. In this case, everything should be a narrow line. If you are not dealing with platforms that do not support Unicode, then everything should probably be widespread.

The _T and TEXT macros are given in the Windows headers for this purpose: support for a single code base that can be compiled for both Windows NT that supports Unicode and Windows 9x that does not support Unicode.

I canโ€™t imagine why you need such a macro if you have not yet included the Windows headers, but if you do, just write it yourself. Except that you need to know when string literals should be wide strings and when they should be narrow strings. There is no "standard" #define . Windows headers use the UNICODE preprocessor character, but you cannot rely on this being defined on other platforms. So, now you are back to where you started.

Why do you think you need a macro for this again? If you strictly encode the string type as wchar_t* , then you will always want to use a wide character literal, so you always want to use the L prefix.

When you use _T and / or TEXT macros from Windows headers, you also do not hardcode the string type as wchar_t* . Instead, you use the TCHAR macro, which automatically resolves the appropriate character type depending on the definition of the UNICODE character.

+1
source

I had a similar problem and I found this solution:

 #define HELLOWORLD "Hello World" const wchar_t* test = L"" HELLOWORLD; 

I tested this in GCC 7, where I needed to make sure the conversion functions (wide in char) work.

I am sure this will work with other compilers as well. Even with the old ones, since functionality is necessary for writing multi-line comments. But perhaps he will complain about using an empty string literal.

0
source

All Articles