https://bugs.llvm.org/show_bug.cgi?id=49621

            Bug ID: 49621
           Summary: std::shuffle() hangs with clang but works well with
                    gcc/msvs
           Product: new-bugs
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected]

Confirmed on Linux and Mac. Discovered with Michail Kalashnikov.

clang -v
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/

Reproducer: https://rextester.com/ZMML24952

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

using namespace std;

int shuffler_fn(int i) {
    //cout << "Shuffler: " << i << endl;
    return std::rand() % i;
}

void random_shuffle() {
    vector<int> rows{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

    std::random_shuffle(rows.begin(), rows.end(), shuffler_fn);

    for (const auto& item : rows) {
        cout << item << ' ';
    }
}

class Shuffler {
public:
  typedef int result_type;
  static constexpr int min() { return 0; }
  static constexpr int max() { return RAND_MAX; }

  int operator()() const { return std::rand(); }
};

void just_shuffle() {
    vector<int> rows{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

    Shuffler shuffler;

    std::shuffle(rows.begin(), rows.end(), shuffler);

    for (const auto& item : rows) {
        cout << item << ' ';
    }
}

int main()
{
    random_shuffle();
    cout << endl;
    just_shuffle();
    cout << endl;
    return 0;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to