c++ - 在 C++ 源代码中使用 Unicode

标签 c++ unicode character-encoding standards

C++ 源代码的标准编码是什么? C++ 标准是否对此有所说明?我可以用 Unicode 编写 C++ 源代码吗?

例如,我可以在评论中使用汉字等非ASCII字符吗?如果是这样,是否允许完整的 Unicode 或只是 Unicode 的子集? (例如,那个 16 位的首页或其他名称。)

此外,我可以对字符串使用 Unicode 吗?例如:

Wstring str=L"Strange chars: â Țđ ě €€";

最佳答案

C++ 中的编码相当复杂。这是我的理解。

每个实现都必须支持来自基本源字符集的字符。其中包括第 2.2/1 节(C++11 中的第 2.3/1 节)中列出的常见字符。这些字符都应该适合一个 char。此外,实现必须支持一种使用称为 universal-character-names 的方式命名其他字符的方式,并且看起来像 \uffff\Uffffffff并且可以用来指代 Unicode 字符。它们中的一个子集可用于标识符(在附件 E 中列出)。

这一切都很好,但是从文件中的字符到源字符(在编译时使用)的映射是实现定义的。这构成了所使用的编码。以下是它的字面意思(C++98 版本):

Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set (introducing new-line characters for end-of-line indicators) if necessary. Trigraph sequences (2.3) are replaced by corresponding single-character internal representations. Any source file character not in the basic source character set (2.2) is replaced by the universal-character-name that des- ignates that character. (An implementation may use any internal encoding, so long as an actual extended character encountered in the source file, and the same extended character expressed in the source file as a universal-character-name (i.e. using the \uXXXX notation), are handled equivalently.)

对于 gcc,您可以使用选项 -finput-charset=charset 更改它。此外,您可以更改用于在运行时表示值的执行字符。正确的选项是 -fexec-charset=charset for char(默认为 utf-8)和 -fwide-exec-charset=charset(根据 wchar_t 的大小,默认为 utf-16utf-32)。

关于c++ - 在 C++ 源代码中使用 Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/331690/

相关文章:

python - 即使在设置 LANG 和 LC_ALL 后,在 mod_wsgi 中运行时也会出现 UnicodeEncodeError

database - 将 unicode 文本插入 Oracle 数据库表

javascript - 将 html 附加到 dom 不尊重字符集

postgresql - 如何获取PostgreSQL中任何字符的代码点和utf 8编码

python - 完全相同的文本字符串不匹配

android - 未定义的函数引用 Android NDK

c++ - 何时使用 pthread_mutex_t

unicode - Erlang、io_lib 和 unicode

c++ - 在特定位置分配多态类成员?

c++ - MS visual studio 2013 SDL 不工作