gcc 6 是否支持使用 std::sample (c++17)?

Does gcc 6 support the use of std::sample (c++17)?

我正在尝试使用 gcc 版本 6.3.0 编译包含 std::sample 的这段 c++ 17 代码使用以下命令:g++ -std=gnu++17 -c main.cpp.

但我明白了:error: ‘sample’ is not a member of ‘std’...

#include <vector>
#include <algorithm>
#include <random>

int main()
{
    std::vector<int> a{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> b(5);

    std::sample(a.begin(), a.end(), 
                b.begin(), b.size(),
                std::mt19937{std::random_device{}()});

    return 0;
}

gcc 6 是否支持使用 std::sample? (使用 gcc 8.2.0 编译良好)

我在这两页上找不到答案:

没有。从"Library Fundamentals V1 TS Components: Sampling"下的table in the documentation可以看出,最早支持std::sample的libstdc++版本是7.1

Does gcc 6 support the use of std::sample?

没有。您需要 GCC 7。来自 GCC 7 release notes:

  • Experimental support for C++17, including the following new features:

    • ...

    • std::sample, std::default_searcher, std::boyer_moore_searcher and std::boyer_moore_horspool_searcher;

对于 GCC 7,您可能需要 -std=c++1z-std=gnu++1z,因为它是实验性的。

是的,因为 GCC 5, but until GCC 7 它在 std::experimental 命名空间中并在 <experimental/algorithm> header 中定义。

来自 GCC 5 发行说明:

Runtime Library (libstdc++)

  • Improved experimental support for the Library Fundamentals TS, including:

    • function template std::experimental::sample;

在 GCC 5.1 上测试https://wandbox.org/permlink/HWnX3qSgKbZO2qoH