Clang++16 can't find sort() from std::ranges on FreeBSD 13.2

Issue

I am trying to compile a program that uses std::ranges::sort() with clang++16, but I am receiving an error when compiling.

According to https://en.cppreference.com/w/cpp/compiler_support/20, the One Ranges Proposal is implemented in Clangs’ libc++ version 13 (partial), 15 (requires -fexperimental-library flag), and 16 (without the flag). I installed clang++16 with sudo pkg install llvm16, but when trying to compile the application it errors out with “no matching function for call to ‘sort’”.

Minimal Reproducible Example

#include <algorithm>
#include <ranges>
#include <array>

int main()
{
    std::array s {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};

    std::ranges::sort(s);
}

Trying to compile with clang++16 -std=c++20 -stdlib=libc++ test.cpp -o test results in this error:

test.cpp:9:15: error: no type named 'sort' in namespace 'std::ranges'
        std::ranges::sort(s);
        ~~~~~~~~~~~~~^
1 error generated.

Question

What am I doing wrong?

The issue you are facing is that std::ranges::sort() is not yet implemented in Clang++ version 16 without the -fexperimental-library flag.

To resolve this issue, you have two options:

  1. Use the -fexperimental-library flag when compiling your code with Clang++ version 16. This flag enables experimental features, including the One Ranges Proposal, which includes std::ranges::sort(). Modify your compilation command to include this flag:

    clang++16 -std=c++20 -stdlib=libc++ -fexperimental-library test.cpp -o test
    
  2. Alternatively, you can use a different compiler that supports the One Ranges Proposal without any additional flags. For example, you can use GCC with the following command:

    g++ -std=c++20 test.cpp -o test
    

Either of these options should allow you to compile your code successfully.