9

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
  • 334
  • 1
  • 2
  • 9

3 Answers3

6

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

Ted Lyngmo
  • 60,763
  • 5
  • 37
  • 77
  • 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
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
  • 512
  • 3
  • 11
  • 1
    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
0

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
  • 21
  • 5