我试图在我的M1 MacBook Pro 13”上启用C20,但没有结果。我正在学习C的教程,老师告诉我如何在Mac上启用C++20(因为他没有Mac,所以在教程中没有实际操作)。所以我在VSCode网站上查找了如何在VSC中使用clang,并设置了VSC。
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"-std=c++20",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": "build",
"detail": "compiler: /usr/bin/clang++"
}
]
}
这是我的tasks.json文件
教师告诉运行以检查C++20是否为
#include <iostream>
int main(int argc, const char **argv)
{
int result = (10 <=> 20) > 0;
return 0;
}
这就是我得到的错误。
➜ clang++ main.cpp
main.cpp:10:22: warning: '<=>' is a single token in C++20; add a space to avoid a change in behavior [-Wc++20-compat]
int result = (10 <=> 20) > 0;
^
main.cpp:10:24: error: expected expression
int result = (10 <=> 20) > 0;
^
1 warning and 1 error generated.
请帮帮我
1条答案
按热度按时间qvsjd97n1#
您遇到的编译器错误是由于使用了较旧的标准(可能是c17?)设置并尝试使用该语言的较新版本中引入的功能。
三路比较('宇宙飞船')运算符是在c20中引入的(参见https://en.cppreference.com/w/cpp/language/operator_comparison),需要针对该标准。
此问题似乎源于
c_cpp_properties.json
配置不当。你可以在VSCode中通过在命令面板中输入'c++ edit configurations'来访问它。在此之后,确保将
"cppStandard"
设置为"c++20"
。您的json
最终应该看起来像这样:(from https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference,已更新
"cppStandard"
)