Using Wide Character Constants with clang Gets “ex

2019-08-28 10:58发布

问题:

I recently decided to switch to clang from gcc and I’m getting the following warning for my use of wide character constants: "extraneous characters in wide character constant ignored". Here is the code that gets the warning:

wstring& line;
…
for (wstring::iterator ch = line.begin(); ch != line.end(); ++ch)
    switch (*ch) {
        case L'│': *ch = L'|'; break;
        case L'﹤': *ch = L'<'; break;
        case L'﹥': *ch = L'>'; break;
        case L'﹙': *ch = L'('; break;
        case L'﹚': *ch = L')'; break;
        default: break;
    }

Here, the characters in the case conditions are all high-unicode characters and therefore seen as multibyte characters by the clang parser, apparently (the source code is UTF-8 encoded).

My question is what is the meaning behind the warning message. That is, what exactly is being ignored. Also, given this warning, will my program work as designed?

gcc does not give any warnings for this code and everything works like a charm.

回答1:

At the heart of the program is the interpretation of the source file. You know that it's UTF-8 encoded. That's why the 6 bytes L'﹤' are to be interpreted as 4 Unicode characters. But how would clang know? It sees 6 bytes, and assumes an 8 bit encoding. Thus, it sees L'xyz' (the precise characters depend on the assumed 8 bit character set). clang tells you that it is interpreting L'xyz' as L'x' , ignoring y and z. It's extremely unlikely that works as intended.