54

I keep on getting error squiggles on std::string_view, but I am able to build just fine. Is there a way to tell intellisense or the C++ linter to use C++17?

The specific error I get is:

namespace "std" has no member "string_view"
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
ajoseps
  • 1,871
  • 1
  • 16
  • 29

9 Answers9

66

This has become much easier now. Search for cppstandard in your vs code extension settings and choose the version of C++ you want the extension to use from the drop down.

enter image description here

In order to make sure your debugger is using the same version, make sure you have something like this for your tasks.json, where the important lines are the --std and the line after that defines the version.

{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++ build active file",
      "command": "/usr/bin/g++",
      "args": [
        "-std=c++17",
        "-I",
        "${fileDirname}",
        "-g",
        "${fileDirname}/*.cpp",
        "-o",
        "${workspaceFolder}/out/${fileBasenameNoExtension}.o"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ],
  "version": "2.0.0"
}

Note that if you're copying the above tasks.json directly, you'll need to have a folder named out in your workspace root.

273K
  • 29,503
  • 10
  • 41
  • 64
giraffesyo
  • 4,860
  • 1
  • 29
  • 39
20

There's a posting in their GitHub issue tracker about this: std::string_view intellisense missing (CMake, VC++ 2017).

In another issue, it is said that the extension defaults to C++17, but does not yet support all of C++17 features: Setting C++ standard.

This is confirmed by c_cpp_properties.json Reference Guide, where an option is listed cppStandard which defaults to C++17. (To edit this file, press Ctrl + Shift + P and type in C/CPP: Edit Configurations).

It appears, then, they just don't have full support yet.

Marc.2377
  • 7,807
  • 7
  • 51
  • 95
  • That's what I was worried about. I saw that github issue as well, but it seems like the responder to the initial post was not able to reproduce the issue. – ajoseps Mar 21 '18 at 19:51
  • 3
    Funny, mine is set at c++17, and still it's not finding `` :angry: – jaques-sam Sep 17 '20 at 12:39
  • 2
    @DrumM That is exactly what I was looking for too, . I can compile, but it is annoying that VS Code gives me squiggles. Oh well. – RTHarston Sep 23 '20 at 21:54
  • 1
    UPDATE: I found a fix. I tried putting -std=C++17 in the "defaults" section of msvc.json per the recommendation of the "Setting C++ standard" link and it didn't work. Problem was I put it in the wrong file. I changed the file in .vscode folder in my Windows' user folder, but I am using VS Code in WSL, so I had to edit the msvc.json file in my WSL user folder's .vscode folder. When I did that the red squiggly went away! – RTHarston Sep 23 '20 at 22:01
  • 2
    I haven't seen any doc about the `msvc.json` file, only the `c_cpp_properties.json`. Why is the latter not sufficient? – jaques-sam Sep 26 '20 at 12:31
7

Just an updated. I got this issue as well.

I solve it by adding c_cpp_properties.json

  1. Ctrl + Shift + P then select C/C++:Edit Configurations (JSON)

  2. Adjust the content for cStandard and cppStandard:

        "cStandard": "gnu17",
        "cppStandard": "gnu++17",
    
W Kenny
  • 1,855
  • 22
  • 33
3

For people trying out on Linux and having GCC 7.5.0 installed, this worked for me.
Do these two steps to enable the linter to acknowledge the c++17 writings and for the compiler to pick up the c++17.

  1. Open up the C/C++:Edit Configurations (JSON), and change the default values for these two fields to:

"cStandard": "gnu18", "cppStandard": "gnu++17",

  1. Open up the tasks.json file inside .vscode directory and add the following statements to the args key:

"--std", "c++17"

Saurav Joshi
  • 414
  • 5
  • 13
2

If you're unable to enable even after trying the solutions by @Marc.2377 and @W Kenny, do the following

  1. Open tasks.json in the .vscode folder
  2. Add "--std","c++17" under "args:"
  3. Save tasks.json
giraffesyo
  • 4,860
  • 1
  • 29
  • 39
pkdyn
  • 189
  • 2
  • 6
1

After trying many things I have found probably a solution for people using CMake and willing to edit the CMakeLists.txt file.

I just put the following line at the beginning of my CMakeLists.txt

set (CMAKE_CXX_STANDARD 17)

You can check your c++ version by doing: cout << __cplusplus ; and the 3rd and 4th number gives you the version of c++ you are using.

For example:

cout << __cplusplus ;

201703

means you are using c++ 17

and

cout << __cplusplus ;

201402

means you are using c++ 14

I think that there must be an easier solution but I could not find it yet.

desmond13
  • 2,913
  • 3
  • 29
  • 46
1

Additional to set cppStandard to gnu++17 in c_cpp_properties.json mentioned in other posts, you need to change the __cplusplus-define to the corresponding value (e.g. 201703L)

Like that:

{
  "version": 4,
  "configurations": [
    {
      // ...
      "cStandard": "gnu17",
      "cppStandard": "gnu++17",
      "defines": [
        // ...
        "__cplusplus=201703L"
        // ...
      ]
    }
  ]
}
  • This should not be needed / this macro should be set automatically if the `-std` argument is set correctly; see https://stackoverflow.com/questions/26089319/is-there-a-standard-definition-for-cplusplus-in-c14 – Philipp Ludwig Jul 09 '22 at 12:23
0

I have tried editing the settings of C_Cpp>Default: Cpp Standard and C Standard to specify them as C++17 standard. However, that did not work.

Then I found a .vscode folder in my project directory with a c_cpp_properties.json file. In there I found settings that I had not edited, where I specified to use the C++11 standard. When I changed it, I found that the problem was gone.

If you find that the answer above doesn't work, maybe we made the same mistake.

王政乔
  • 1
  • 1
-1

Check your version of g++ using g++ --version on the command line. If it is like version 6 or 7 then you need to update to a newer version with mingw. I used mysys2 to do this and now I do not have the same problem.

Tariq Soliman
  • 39
  • 1
  • 4