簡體   English   中英

我如何在 vscode 中使用 C++20?

[英]How do I use C++20 in vscode?

我想在 vscode 中使用 C++20,因為我想在 unordered_set 上使用.contains,但是當我嘗試它時,我收到error C2039: 'contains': is not a member of 'std::unordered_set而且我不知道不明白為什么我已經轉到 c_cpp_properties.json 並指定使用 c++20 但它似乎仍然不起作用,而且我在 vscode 上找不到任何關於更改 C++ 版本的信息。

編譯器版本: 19.25.28614 for x86

您必須添加 msvc 編譯器選項/std:c++latest才能使用unordered_map::contains()成員 function。

此頁面上提供了一個很好的說明:

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"
}

據我所知,c_cpp_properties.json 中關於 c++ 版本的設置僅用於幫助您編寫代碼的服務(智能感知、代碼瀏覽等)。 Vscode 沒有自己的 c++ 編譯器。 它使用您配置的任何編譯器。

您可能想檢查編譯器支持的最新標准。 我發現這篇文章很有幫助。 如何確定編譯器使用的C++標准的版本?

確保使用編譯器(編譯時或運行時)評估常量。 當您 hover 和 cursor 時,您可能會看到不同的值。

這是 windows 的 tsaks.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 你也可以做到的。 100% 工作。

{
        "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"
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM