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:

6

3 Answers 3

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

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

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.