c++gccc++17gcc6

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


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:


Solution

  • 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