'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
Solution 1:[1]
You must add the msvc compiler option /std:c++latest
to be able to use the unordered_map::contains()
member function.
Solution 2:[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.
Solution 3:[3]
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"
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ted Lyngmo |
Solution 2 | DeltA |
Solution 3 | wave-rider |