我该如何在VS Code中使用C++20?

14
我想在VS Code中使用C++20,因为我想在unordered_set上使用.contains方法,但当我尝试时,出现了以下错误: error C2039: 'contains': is not a member of 'std::unordered_set' 我不明白为什么会这样,因为我已经进入了c_cpp_properties.json并指定了使用c++20,但它似乎仍然无法工作,并且我找不到任何有关在VS Code上更改C++版本的信息。
编译器版本:19.25.28614 for x86

你似乎正在使用Visual Studio编译器?是哪个版本?它支持你需要的C++20特性吗? - Alan Birtles
2
设置 "cppStandard": "gnu++20" 是另一个选项。 - Ahmet
4个回答

7

你需要添加msvc编译器选项 /std:c++latest,才能使用unordered_map::contains() 成员函数。


对于MSVC编译器,如果编译器路径当前是文件路径,我需要输入什么?我是否只需用/ std :: c ++ latest替换文件路径?我已经用/ std:c ++ latest替换了路径,但是我收到“找不到”std:c ++ latest“”的错误提示,并且IntelliSense模式msvc-x64与编译器路径不兼容。 - Justin Chee
1
@JustinChee,你不应该更改编译器路径(可能以 cl.exe 结尾)。/std:c++latestcl.exe 程序的一个选项。在 c_cpp_properties.json 文件中,你可以尝试:"cppStandard": "c++latest"。如果这样还行不通,你可以添加一个 compileCommands 条目,并在 compile_commands.json 文件中指定额外的选项。 - Ted Lyngmo

5
这是Windows的tasks.json文件。只需编辑args并添加"-std=c++23",就完成了!我已经从winlibs离线安装了winlibs-x86_64-posix-seh-gcc-12.2.0-llvm-15.0.7-mingw-w64ucrt-10.0.0-r4。你也可以这样做。百分之百可行。
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-std=c++23",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

3
据我所知,c_cpp_properties.json 中关于 C++ 版本的设置仅用于帮助您编写代码的服务(智能感知、代码浏览等)。 VS Code 没有自己的 C++ 编译器。它使用您配置的任何编译器。
您可能想要检查编译器支持的最新标准。我发现这篇文章非常有帮助。请查看链接:如何确定编译器使用的 C++ 标准版本? 请确保使用编译器(编译时或运行时)评估常量。当您将光标悬停在其上时,您可能会看到不同的值。

2
是的,在这里使用g++,在扩展设置中将C_Cpp › 默认值:Cpp标准设置为c++20,可以消除波浪线警告。我手动在我的tasks.json文件中的args中添加了“-std=c++20”以使其采用编译指令。 - Don Slowik

1
这个页面提供了很好的指导:

https://code.visualstudio.com/docs/cpp/config-msvc

。缺少的部分是cl.exe所需的 /std:c++20 编译器标志。这是一个更新的 tasks.json,对我有用:
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/std:c++20",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接