c++ u8字面值字符覆盖

c++ u8 literal char override

本文关键字:覆盖 字符 字面值 u8 c++      更新时间:2023-10-16

我尝试重写字符量,以便通过使用uuLu8前缀使它们char16_twchar_tchar32_tchar。它适用于除u8以外的所有对象。见下文:

#include <iostream>
using std::cout; using std::endl;
int main(){
cout<<"'a'----"<<'a'<<endl;
cout<<"L 'a'----"<<L'a'<<endl;
cout<<"u 'a'----"<<u'a'<<endl;
cout<<"U 'a'----"<<U'a'<<endl;
//cout<<"u8 'a'----"<<u8'a'<<endl;
return 0;
}

Consolse输出:

'a'----a
L 'a'----97
u 'a'----97
U 'a'----97

当我取消注释u8行时,我得到以下错误:

'u8' was not declared in this scope prog.cpp    

你知道为什么它不工作吗?

参见http://www.cplusplus.com/doc/tutorial/constants/

u8用于字符串字面值,而u、u和L用于字符字面值。

在下一行中,您将u8前缀添加到字符字面量。cout<& lt;"与""——"& lt;

u8字符是c++ 17的特性。确保你的编译器支持它并且你已经启用了它