Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ variant No such file or directory

Tags:

c++

g++

variant

I have a problem compiling code using std::variant. I try to compile this code with g++ 5.4/6.2 on ubuntu and fedora with -std=c++17:

#include <variant>
#include <string>

int main()
{
    std::variant<int, float> v, w;
    v = 12; // v contains int
    int i = std::get<int>(v);
    w = std::get<int>(v);
    w = std::get<0>(v); // same effect as the previous line
    w = v; // same effect as the previous line

    try {
      std::get<float>(w); // w contains int, not float: will throw
    }
    catch (std::bad_variant_access&) {}

    std::variant<std::string> v("abc"); // converting constructors work when unambiguous
    v = "def"; // converting assignment also works when unambiguous
}

found on cppreference.com but this error append: "fatal error: variant: No such file or directory".

like image 404
Vuillemot Florian Avatar asked Feb 06 '23 21:02

Vuillemot Florian


1 Answers

  1. Install g++-7; e.g. if ubuntu
    sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y sudo apt-get update sudo apt-get install g++-7
  2. run /usr/bin/g++-7 -std=c++1z your_program.cc

  3. Optional, if you use cmake system, add these lines :
    set(CMAKE_CXX_COMPILER "/usr/bin/g++-7") set(CMAKE_CXX_FLAGS "-std=c++1z")

like image 170
user1823890 Avatar answered Feb 08 '23 15:02

user1823890