所以我想char8_t在我的代码中使用数据类型。但我的编译器向我显示它找不到像 这样的标识符char8_t,这可能意味着它找不到所需的头文件或定义。
那么谁能告诉我应该使用哪些头文件/定义来获取char8_t数据类型?语言是C++. 如果您也能回答,我将不胜感激C。
PS:<cwchar>没用。
编辑:
我的代码:
#include <iostream>
#include <cwchar>
using namespace std;
int main()
{
char c='a';
wchar_t w=L'A';
char8_t c8=u8'c';
char16_t c16=u'D';
char32_t c32=U'K';
cout<<"Data Type"<<"\t"<<"Size"<<"\n"
<<"char"<<"\t \t"<<sizeof(c)<<" bytes"<<"\n"
<<"wchar_t"<<"\t \t"<<sizeof(w)<<" bytes"<<"\n"
<<"char8_t"<<"\t"<<sizeof(c8)<<"\n"
<<"char16_t"<<"\t"<<sizeof(c16)<<" bytes"<<"\n"
<<"char32_t"<<"\t"<<sizeof(c32)<<" bytes"<<"\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的编译器抛出这个错误:
WideCharacters.cpp: In function 'int main()':
WideCharacters.cpp:8:5: error: 'char8_t' was not declared in this
scope; did you mean 'wchar_t'?
8 | char8_t c8=u8'c';
| ^~~~~~~
| wchar_t
WideCharacters.cpp:15:31: error: 'c8' was not declared in this
scope; did you mean 'c'?
15 | <<"char8_t"<<"\t"<<sizeof(c8)<<"\n"
| ^~
| c
Run Code Online (Sandbox Code Playgroud)
编辑2:
我在 VS 代码中将 C++ 标准设置为 C++20,因此标准没有问题。
char8_t是一个关键字。它内置于语言中,因此您不需要任何标头。
如果它不起作用,要么您的编译器不支持它,要么您忘记启用 C++20 支持(例如-std=c++20在 GCC 中)。