[Interest] Is it safe to use QString::utf16() as a Windows wchar_t*?
Nikos Chantziaras
realnc at gmail.com
Fri Nov 10 10:04:25 CET 2017
On 09/11/17 15:00, Konstantin Tokarev wrote:
>
>
> 09.11.2017, 12:16, "Nikos Chantziaras" <realnc at gmail.com>:
>> On Windows, I use this:
>>
>> void func(const wchar_t*);
>> QString s;
>>
>> // ...
>>
>> func(reinterpret_cast<const wchar_t*>(s.utf16()));
>>
>> This saves an allocation, a copy and a free since I don't have to use
>> QString::toWCharArray(). However, is this actually safe? "It seems to
>> work," and AFAIK, QString::d->data() is already in the format and
>> encoding Windows excepts a wchar_t* to be. Is this correct?
>
> Yes, if you make sure this code is used only on Windows.
> You may want to add static assert that sizeof wchar_t == sizeof ushort
Yep, it's Windows-only. Although I doubt wchar_t could be anything else
than a two-byte integral type on Windows (that would break the
Universe,) I added static_asserts for sizeof and alignof.
More information about the Interest
mailing list