首页 > 解决方案 > 如何修复 lib libstdc++ 的 `std::variant`(来自 GCC 9.1)不适用于 Clang 8?

问题描述

MSYS2 的 GCC 包最近更新到 9.1,但是 Clang 不喜欢<variant>它附带的新 libstdc++ 头文件。

编译以下简单程序时:

#include <variant>

int main()
{
    std::variant<int, float> x;
}

我得到:

# clang++ -std=c++17 foo.cpp

In file included from foo.cpp:1:
Z:\...\msys2\mingw64\include\c++\9.1.0\variant:1559:55: error: '__get' is missing exception specification 'noexcept'
        friend constexpr decltype(auto) __detail::__variant::__get(_Vp&& __v);
                                                             ^
foo.cpp:5:30: note: in instantiation of template class 'std::variant<int, float>' requested here
    std::variant<int, float> x;
                             ^
Z:\...\msys2\mingw64\include\c++\9.1.0\variant:263:5: note: previous declaration is here
    __get(_Variant&& __v) noexcept
    ^
1 error generated.

<variant>如果您想查看它,这是完整的标题。

在等待官方修复时,我按照 Clang 的建议进行操作并添加noexcept到标题中。

到目前为止它似乎有效。

这个解决方案会导致任何问题吗?我应该做点别的吗?

如果您知道它是 libstdc++ 错误还是 Clang 错误,则可以加分。

标签: c++clangclang++libstdc++

解决方案


修复是正确的。这是一个 libstdc++ 错误,请参阅https://bugs.llvm.org/show_bug.cgi?id=41863https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90397


推荐阅读