c++qtwchar-tlpwstr

Never seen this syntax before, can someone explain..... return L""


Its been difficult trying to find an explanation online on the below syntax and searching for L"" in these forums doesn't provide any results. I'm inside Qt5 debugging an application which is too lengthy to paste here. I'm trying to resolve this last error that points to the line of code containing --> return L""; <--. Its one of the last lines in the function below. The compiler error i'm receiving is...

C2440: 'return': cannot convert from 'const wchar_t[1]' to 'LPWSTR'

My first question is what does this mean --> L"" <-- ?

Lastly, how can I resolve this compiler error?

LPWSTR ImageThread::getLastErrorString(DWORD lastError)
    {
        DWORD result = FormatMessage(
            FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            lastError,
            LANG_USER_DEFAULT,
            errorString,
            sizeof(errorString),
            NULL);
        if (result > 0)
        {
            return errorString;
        }
        else
        {
            return L"";
        }
    }

My assumption is because wchar_t[1] is 1 byte and LPWSTR is 2 bytes it's not able to convert??? I'm not for sure if this is important but I'm getting another Error up in the tool bar of Qt5 saying "Error: Could not decode "myfile.cpp" with "UTF-8" encoding. Editing not possible". It gives me an option to select Encoding. I'm sure this is due to the fact that this code was written with an older version of Qt and now because I'm rebuilding all this code with Qt5 there has been some changes. Again, I'm not for sure this relates to my two questions above but I wanted to paint a complete picture of what's going on.


Solution

  • An "L" (or 'l', though most recommend against it, because of how much it looks like a 1) before a string literal (or character literal) makes it a "wide" literal, so what it contains are wchar_t instead of char, and its address is a wchar_t * instead of a char *.

    The C and C++ standards don't specify precisely what wide characters are. On Windows, they'll normally be 16 bits representing the string in UTF-16. On most Unix-like systems, they'll normally be 32 bits, representing the string in UCS-4.

    In this particular case, the "" after the L means it's just returning an empty string.