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

7
我正试图编译这段包含std::samplec++ 17代码,使用的是gcc版本6.3.0和下面的命令: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下编译正常)

我在以下两个页面上找不到答案:


它可以在MinGW g++ 7.3.0上编译通过。如果这是问题的原因,那么你可以在Windows上使用这个版本。 - Cheers and hth. - Alf
2
https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017 - Justin
顺便说一句,Windows 已经有了 GCC 8.1(MinGW-w64)。 - HolyBlackCat
2
https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017 是检查的地方。答案(显而易见):不是。 - Shawn
@HolyBlackCat:你有链接吗?还是你在谈论使用Linux环境,例如Windows 10的WSL和交叉编译? - Cheers and hth. - Alf
@Cheersandhth.-Alf 不,我不是在谈论使用Linux环境。这是链接。 - HolyBlackCat
3个回答

5
是的,自GCC 5起,但直到GCC 7它在std::experimental命名空间中,并在<experimental/algorithm>头文件中定义。
来自GCC 5发行说明:

运行时库(libstdc++)

  • 改进了对库基础TS的实验性支持,包括:

    • 函数模板std::experimental::sample;
在GCC 5.1上进行了测试:https://wandbox.org/permlink/HWnX3qSgKbZO2qoH

3

不行。我们可以从文档中的“Library Fundamentals V1 TS Components: Sampling”部分的表格中看出,支持std::sample的最早版本是7.1版的libstdc++。


1

gcc 6支持使用std::sample吗?

不支持。你需要使用GCC 7。来自GCC 7 release notes:

  • 实验性支持C++17,包括以下新功能:

    • ...

    • std::sample、std::default_searcher、std::boyer_moore_searcher和std::boyer_moore_horspool_searcher;

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接