c++ 变体没有这样的文件或目录

c++ variant No such file or directory

我在使用 std::variant 编译代码时遇到问题。 我尝试在 ubuntu 上使用 g++ 5.4/6.2 编译此代码,在 -std=c++17:

上使用 fedora 编译此代码
#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 上找到,但此错误附加:"fatal error: variant: No such file or directory"。

std::variant 是在 C++17 中添加的。

gcc 还没有完全支持 C++17 标准的相关位。

我什至没有在 gcc's tracking page 中看到 std::variant

  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. 运行 /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")