Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 203
Justin Chee Avatar asked May 30 '20 09:05

Justin Chee


3 Answers

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

like image 198
Ted Lyngmo Avatar answered Sep 17 '22 13:09

Ted Lyngmo


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.

like image 41
DeltA Avatar answered Sep 17 '22 13:09

DeltA


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"
}
like image 41
wave-rider Avatar answered Sep 18 '22 13:09

wave-rider