13

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

Justin Chee
  • 427
  • 1
  • 3
  • 14

4 Answers4

7

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

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • for msvc compiler, what would I have to type as the compiler path is currently a file path so would I just replace the file path with /std::c++latest ? I replaced the path with /std:c++latest but I get "Cannot find "std:c++latest"" and IntelliSense mode msvc-x64 is incompatible with compiler path. – Justin Chee May 30 '20 at 10:01
  • 1
    @JustinChee You shouldn't change the compiler path (which probably ends with: `cl.exe`). `/std:c++latest` is an option to the program `cl.exe`. In `c_cpp_properties.json` you could try: `"cppStandard": "c++latest"`. If that doesn't work, you can add a `compileCommands` entry and specify the extra option in a `compile_commands.json` file. – Ted Lyngmo May 30 '20 at 10:20
3

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"
}
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
2

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.

DeltA
  • 564
  • 4
  • 12
  • 2
    Yes, using g++ here, setting C_Cpp › Default: Cpp Standard to c++20 in Extensions Settings just made the squiggle warning go away. I manually added "-std=c++20", to args in my tasks.json in .vscode to get it to take on the compile instruction. – Don Slowik Sep 15 '21 at 17:29
1

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"
}
wave-rider
  • 31
  • 5