What is the fastest way to get "const wchar_t *" from QString

I want to pass a variable QStringto a function with an argument const wchar_t*.

Safe solution:

void foo(const wchar_t*);
QString x = "test";
foo(x.toStdWString().c_str());

but it has conversion overhead in wstring. Is there a faster solution?

How about this solution? Is it safe and tolerated?

foo(reinterpret_cast<const wchar_t*>(x.constData()));
+4
source share
2 answers

No, the reinterpret is not safe and will not work on some platforms where the encoding is different. QChar- 16 bits, and the internal encoding - UTF-16, but Linux wchar_tuses 32-bit encoding UCS-4.

( - undefined , ms++ , ) Windows, wchar_t 16 , Qt , ?

, std::wstring , . , , std::wstring ( ++ 98), std::wstring::c_str() - getter.

+4

x.constData , const wchar_t*, .

, , ( , , ).

, - x.utf16() ( ), , wchar_t unsigned short. . http://qt-project.org/doc/qt-4.8/qstring.html#utf16

QString UTF-16, , wchar_t , .

-1

Source: https://habr.com/ru/post/1536894/


All Articles