7

I'm trying to compile this piece of c++ 17 code that contains std::sample using gcc version 6.3.0 with the following command: g++ -std=gnu++17 -c main.cpp.

But I get this: 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;
}

Does gcc 6 support the use of std::sample? (It compiles fine with gcc 8.2.0)

I could not find the answer on this two pages:

T.L
  • 595
  • 3
  • 16
  • It compiles fine with MinGW g++ 7.3.0 That's a version you can use in Windows, if that's the problem. – Cheers and hth. - Alf Sep 10 '18 at 22:34
  • 2
    https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017 – Justin Sep 10 '18 at 22:35
  • FWIW, windows already has GCC 8.1 (MinGW-w64). – HolyBlackCat Sep 10 '18 at 22:35
  • 2
    https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017 is the place to check. Answer (Which should be obvious): No. – Shawn Sep 10 '18 at 22:36
  • @HolyBlackCat: Do you have a link, or are you talking about using a Linux environment such as Windows 10's WSL, and cross-compiling? – Cheers and hth. - Alf Sep 10 '18 at 23:18
  • @Cheersandhth.-Alf No, I'm not talking about using a Linux environment. [Here's the link.](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe/download) – HolyBlackCat Sep 11 '18 at 05:46

3 Answers3

5

Yes, since GCC 5, but until GCC 7 it is in std::experimental namespace and defined in <experimental/algorithm> header.

From GCC 5 Release notes:

Runtime Library (libstdc++)

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

    • function template std::experimental::sample;

Tested on GCC 5.1 https://wandbox.org/permlink/HWnX3qSgKbZO2qoH

Nikita Kniazev
  • 3,728
  • 2
  • 16
  • 30
3

No. We can see from the table in the documentation under "Library Fundamentals V1 TS Components: Sampling" that the earliest version of libstdc++ to support std::sample is version 7.1

Justin
  • 24,288
  • 12
  • 92
  • 142
1

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

No. You need GCC 7. From the 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;

For GCC 7 you may need -std=c++1z or -std=gnu++1z since it is experimental.

jww
  • 97,681
  • 90
  • 411
  • 885