I've come upon an instance where I need to create a large lookup table of unicode strings, and was curious what the recommended way to handle this was. Using MSVC I would simply declare the string as L"Text"
I only see two options currently, either declare the string as (const WCHAR[]){'T','e','x','t'} or store as non-unicode and use WideCharToMultiByte/MultiByteToWideChar as appropriate
As this is being implemented for a unicode-only function, it seems silly to convert the string, but declaring it as (const WCHAR[]){'T','e','x','t'} is a little unwieldy for the quantity of data that I need to declare
Kevin Koltzau wrote:
I've come upon an instance where I need to create a large lookup table of unicode strings, and was curious what the recommended way to handle this was. Using MSVC I would simply declare the string as L"Text"
I only see two options currently, either declare the string as (const WCHAR[]){'T','e','x','t'} or store as non-unicode and use WideCharToMultiByte/MultiByteToWideChar as appropriate
As this is being implemented for a unicode-only function, it seems silly to convert the string, but declaring it as (const WCHAR[]){'T','e','x','t'} is a little unwieldy for the quantity of data that I need to declare
as the L"Text" construct is not supported inside, as using WideCharToMultiByte will cause memory and CPU consumption (especially for static lookup tables),
the (const WCHAR[]){'T','e','x','t'} (don't forget the trailing '\0') is the preferred. We all know it's a PITA, but it's better to put the constraint once, at code writing time. When I'm faced with this, I'm using some macros in emacs to do the task (ie, from a "text" string convert it to the 't','e','x','t' form)
A+
Eric Pouech pouech-eric@wanadoo.fr writes:
the (const WCHAR[]){'T','e','x','t'} (don't forget the trailing '\0') is the preferred. We all know it's a PITA, but it's better to put the constraint once, at code writing time.
Note that the (const WCHAR[]) cast is not portable, so you shouldn't use that. You need to declare a real array of WCHARs.