c ++ u8文字字符覆盖

时间:2016-10-01 16:05:01

标签: c++ c++11 utf-8 char

我尝试覆盖字符文字,以使它们 char16_t wchar_t char32_t char 使用 u U L u8 前缀。它适用于除 u8之外的所有< / em>的。见下文:

#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    

你知道为什么不起作用吗?

2 个答案:

答案 0 :(得分:1)

u8字符为C++17 feature。确保您的编译器支持它并且您已启用它。

答案 1 :(得分:0)

请参阅http://www.cplusplus.com/doc/tutorial/constants/

u8用于字符串文字,而u,U和L用于字符文字。

在以下行中,您将u8前缀添加到字符文字中。 cout&lt;&lt;&#; u8 \&#39; a \&#39; ----&#34;&lt;