没有这样的文件或目录

c++ variant No such file or directory

本文关键字:文件      更新时间:2023-10-16

我有一个使用std::variant编译代码的问题。我尝试在ubuntu和fedora的g++ 5.4/6.2上编译这段代码,使用-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
}

在cppreference.com上找到,但是这个错误追加:"致命错误:variant: No such file or directory".

  1. 安装g++ 7;例如,如果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. 可选,如果使用cmake系统,添加以下行:
    set(CMAKE_CXX_COMPILER "/usr/bin/g++-7") set(CMAKE_CXX_FLAGS "-std=c++1z")

std::variant在c++ 17中被添加。

gcc还没有完全支持c++ 17标准的相关部分。

我甚至没有在gcc的跟踪页面中看到std::variant