I try to run codes like
#include <string>
#include <iostream>
int main() {
std::string str = "This is a string";
std::cout << str.starts_with("name");
}
But intellisense will give out an error
"std::__cxx11::basic_string<char, std::char_traits, std::allocator>" has no member "starts_with" C/C (135) [6,9]
And It still can be build and produce a correct result.
Also it can find implementation in header file.
But the macro __cplusplus
is defined as 201703L
I've already added a command -std=c 20
when building, why this happened?
Compiler: minGW 11.2 compiled by msys2
CodePudding user response:
Assuming you are using Microsoft's C/C extension, you must configure the extension to use C 20 standard for intellisense.
The easiest way to do this is to add the line "C_Cpp.default.cppStandard": "c 20"
to your settings.json file. You can also find the setting in the GUI under the name "Cpp Standard". Selecting c 20
from its dropdown will achieve the same result.
Note that this setting is, by default, set as a global user defaults. You can configure it per-workspace by selecting the Workspace tab in the settings GUI and changing that Cpp Standard dropdown to c 20
.
As for why adding the -std=c 20 flag didn't work: -std=c 20
just tells your compiler which standard to use to build your code. 'Intellisense' does not receive this flag because it is a separate tool from the compiler and is therefore not required to support all the standards the compiler supports. It may support less even, although Intellisense tools usually support as current a standard as possible. Therefore the language standard for Intellisense must be configured separately from the compiler (in this case).
Final Note: After changing the setting, try closing and re-opening VS Code. In my experience changing the language standard setting can cause some weirdness to happen. Closing and re-opening VS Code seems to ensure the setting changes take full effect.