3

I'd like to call a function that takes a single range parameter, using an iterator-sentinel pair. The recommended method seems to be to call std::ranges::subrange(begin, end). For example, I would use the following:

#include <ranges>
#include <vector>
#include <iostream>

int main() {
    std::vector<int> a{1, 2, 3};
    auto r = std::ranges::subrange(a.begin(), a.end());
    for (int a : r) {
        std::cout << a << " ";
    }
}

However this causes an error when using clang 13 (with -std=c++20):

opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/iterator_concepts.h:980:13: error: no matching function for call to '__begin'
    = decltype(ranges::__cust_access::__begin(std::declval<_Tp&>()));
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

See https://godbolt.org/z/cGfvxo66j . The example does compile with -stdlib=libc++ rather than the default libstdc++, but this gives other issues in my application.

I know ranges support in clang is only partial, but subrange seems like a very basic construction. Is there any other way to convert an iterator-sentinel pair to a range?

Mark
  • 1,306
  • 13
  • 19
  • Ranges are implemented in the standard library, not the compiler, so really you're asking about ranges support in libstdc++, not clang. – Marshall Clow Jan 26 '22 at 18:30
  • 1
    OK, I see. But to clarify, the example does compile with gcc and libstdc++. – Mark Jan 27 '22 at 10:38

0 Answers0