tornikeo
tornikeo

Reputation: 938

VSCode "variant is not a template", how to make vscode detect it?

I am writing a simple structure in using vscode in c++

#include <variant>
using namespace std;
struct Entry {
  string name;
  variant<double,int> v;
};

I am using the C/C++ extension by Microsoft.

Despite the fact that it's compiling just fine, somehow the extension is unable to detect the variant class.

How do I fix this issue with VScode?

Upvotes: 1

Views: 2879

Answers (3)

Ricky Sixx
Ricky Sixx

Reputation: 663

If you are using the clangd extension (not the C++ one by Microsoft), add this in your .vscode/settings.json file:

{
    "clangd.fallbackFlags": [
        "-std=c++17"
    ]
}

Then reload the extesion to apply the changes.

Upvotes: 1

Yongkin
Yongkin

Reputation: 81

For those who use cl.exe (VSCode guide on configuring Microsoft C++) to compile and link C/C++ program:

  • Look for .vscode/task.json.
  • Add "/std:c++latest" to args list.
  • Hit Ctrl + Shift + B to build.

More options regarding cl.exe can be found by typing cl.exe /help in Developer Command Prompt for VS XXXX.

Upvotes: 0

tornikeo
tornikeo

Reputation: 938

Found it!

  • Go to Extensions > C/C++ > Gear Icon > Extension Settings
  • In upper search bar add "standard"
  • Look for the "C_Cpp > Default: Cpp Standard" entry.
  • Click on drop-down list and select the highest c++ standard that you need.
  • Reload and the error goes away.

The Same goes for this kind of problems in C.
It seems like the default c++ standard of this extension can sometimes be insufficient.

Upvotes: 6

Related Questions