Justin Chee
Justin Chee

Reputation: 475

How do I use C++20 in vscode?

I want to use C++20 in vscode as I'd like to use .contains on an unordered_set, but when I try it I get error C2039: 'contains': is not a member of 'std::unordered_set and I don't understand why as I 've already went on to c_cpp_properties.json and specified the use of c++20 but it still doesn't seem to work, and I can't find anything anywhere about changing the C++ version on vscode.

Compiler version: 19.25.28614 for x86

Upvotes: 14

Views: 42187

Answers (4)

Soumyadeep Mondal
Soumyadeep Mondal

Reputation: 61

This is the tasks.json for Windows. Just edit the args and add "-std=c++23" and voila! Your job is done! I've installed offline winlibs-x86_64-posix-seh-gcc-12.2.0-llvm-15.0.7-mingw-w64ucrt-10.0.0-r4 from winlibs. You can do the same. 100% working.

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

Upvotes: 6

wave-rider
wave-rider

Reputation: 31

A good instruction is available on this page:

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

The missing bit is the /std:c++20 compiler flag that is needed for cl.exe

This is an updated tasks.json that worked for me:

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

Upvotes: 1

DeltA
DeltA

Reputation: 584

As of my knowledge, settings about c++ version in c_cpp_properties.json are just used for services that help you write the code (intellisense, code browsing, etc.). Vscode has no c++ compiler of its own. It uses whatever compiler you configured it to.

You might want to check the latest standard your compiler supports. I found this post very helpful. How to determine the version of the C++ standard used by the compiler?

Make sure to evaluate the constant using the compiler (compile-time or run-time). You might see a different value when you hover the cursor on it.

Upvotes: 3

Ted Lyngmo
Ted Lyngmo

Reputation: 117168

You must add the msvc compiler option /std:c++latest to be able to use the unordered_map::contains() member function.

Upvotes: 9

Related Questions