Using Wide Character Constants with clang Gets “extraneous characters in wide character constant ignored” Error

瘦欲@ 提交于 2019-12-12 04:28:55

问题


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.



来源:https://stackoverflow.com/questions/3343553/using-wide-character-constants-with-clang-gets-extraneous-characters-in-wide-ch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!