c++ - 可以在 C++ 中的字符串文字中写入 unicode 字符吗

标签 c++ visual-studio visual-c++ unicode

写下面的代码可以吗?

const char* str = "§some-text";

如果源文件以 UTF-8 编码保存,str 是否会包含 § 字符的正确 UTF-8 表示?

或者唯一的写法是使用 u8 前缀的字符串文字?

最佳答案

您是否可以在源代码中使用 Unicode 字符(而不仅仅是在字符串文字中)是实现定义的。唯一可移植的方法是坚持“基本源字符集”中的字符并使用 u8"\u00a7some-text"

[lex.phases]/1:

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. The set of physical source file characters accepted is implementation-defined. Any source file character not in the basic source character set (2.3) is replaced by the universal-character-name that designates 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 (e.g., using the \uXXXX notation), are handled equivalently except where this replacement is reverted in a raw string literal.)

“基本源字符集”是:

The basic source character set consists of 96 characters: the space character, the control characters representing horizontal tab, vertical tab, form feed, and new-line, plus the following 91 graphical characters:

a b c d e f g h i j k l m n o p q r s t u v w x y z

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

0 1 2 3 4 5 6 7 8 9

_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ~ ! = , \ " ’

关于c++ - 可以在 C++ 中的字符串文字中写入 unicode 字符吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36496414/

相关文章:

c++ - 检测Enter键以获取用于int类型的输入

c# - 设置 Visual Studio 的环境变量以调试项目

c++ - 使用 Visual Studio C++ 进行单元测试时链接器错误

c++ - 标准输入作为 MSVC 的输入文件

c++ - boost::shared_ptr<Type> 不序列化数据并给出错误

c++ - 我如何在 QTreeWidget (Qt) 中绘制不同颜色的行

c++ - 如何在 Bazel 中静态链接系统库?

c++ - Visual Studio Community 2017 LNK2019 Unresolved external symbol

c++ - 将参数传递给参数类对象

c++ - 不同的编译结果 : Mac vs Windows