10

How to write a concept that will describe the types the Range-based for loop is enabled for?

One attempt is:

template < typename Range > concept bool RRange
    = requires(Range range) {{std::begin(range),std::end(range)};};

but what I really want is some thing like this:

template < typename Range > concept bool RRange
    = requires(Range range) {{for(auto&& item : range);};}; // compile error

that is, RRange to be the concept of all types the expression for(auto&& item : range); is valid for. What is the best way to achieve this?

I am using GCC7 snapshot with g++ -std=c++1z -fconcepts.

3 Answers 3

1

Here's what I came up with while reviewing [stmt.ranged].

#include <utility>
#include <experimental/type_traits>

template <class T> using begin_non_mf_t = decltype(begin(std::declval<T>()));
template <class T> using begin_mf_t     = decltype(std::declval<T>().begin());
template <class T> using begin_t        = decltype(T::begin);
template <class T> using end_non_mf_t   = decltype(end(std::declval<T>()));
template <class T> using end_mf_t       = decltype(std::declval<T>().end());
template <class T> using end_t          = decltype(T::end);

template <class T>
constexpr bool has_member_begin_or_end {
    std::experimental::is_detected_v<begin_mf_t,T> ||
    std::experimental::is_detected_v<begin_t,T> ||
    std::experimental::is_detected_v<end_mf_t,T> ||
    std::experimental::is_detected_v<end_t,T>};

template <class T>
std::add_lvalue_reference_t<T>  declref() noexcept;
template <class T> using declref_t = decltype(declref<T>());

template <class T>
concept bool Range =
    requires /*Arrays*/ {
        requires std::is_array_v<T>;
        requires std::extent_v<T>!=0; // Extent is known.
    } ||
    /*Classes with member begin/end*/
    requires {
        requires std::is_class_v<T> && has_member_begin_or_end<T>;
    } &&
    requires (begin_mf_t<declref_t<T>> _begin,
                end_mf_t<declref_t<T>> _end) {
        { _begin!=_end } -> bool;
        { *_begin } -> auto&&;
        { ++_begin };
    } ||
    /*Types with non-member begin/end*/
    requires {
        requires !std::is_class_v<T> || !has_member_begin_or_end<T>;
    } &&
    requires (begin_non_mf_t<declref_t<T>> _begin,
                end_non_mf_t<declref_t<T>> _end) {
        { _begin!=_end } -> bool;
        { *_begin } -> auto&&;
        { ++_begin };
    };

And the test cases.

#include <vector>

// Evaluates to true or diagnoses which constraints failed.
template <Range> constexpr bool is_range {true};

static_assert(!Range<void>);
static_assert(!Range<int>);
static_assert(!Range<int*>);
static_assert(!Range<int[]>);
static_assert(is_range<int[1]>);
static_assert(is_range<std::vector<int>>);

struct A { };
struct B {
    int begin;
};
struct C {
    int* begin();
    int* end();
};
struct D { };
struct E {
    int end;
};
enum F { };
struct G {
    int* begin() &&;
    int* end();
};
struct H {
    int* begin() &&;
    int* end() &&;
};
int* begin(D);
int* end(D);
int* begin(E);
int* end(E);
int* begin(F);
int* end(F);
int* begin(H);
int* end(H);

static_assert(!Range<A>);
static_assert(!Range<B>);
static_assert(is_range<C>);
static_assert(is_range<D>);
static_assert(!Range<E>);
static_assert(is_range<F>);
static_assert(!Range<G>);
static_assert(!Range<H>);

int main() { }
4
  • 1
    It’s good to add a caveat that due to how the spec is written, user code can only make a best-effort attempt at concept-ifying this language feature (although a very good one). There are (very) pathological edge cases that can’t be caught.
    – Luc Danton
    Commented Nov 22, 2016 at 10:23
  • @LucDanton I've rewritten the concept to operate on named variables, as required by the range-for semantics. However, the added test case fails, and I don't know why. Could it be an edge case, like you mentioned earlier? Commented Nov 22, 2016 at 13:59
  • I shouldn't have skipped testing the new test case with the previous solution. I've fixed it through the added declref indirection. Thank you for everything. Commented Nov 23, 2016 at 11:44
  • Is there a centralized place (i.e. a concrete include file) where these type of (standard?) concepts definitions like this are, or are going to be, located?
    – alfC
    Commented Sep 11, 2018 at 19:50
0

According to P0587, this should suffice:

#include <vector>

template<typename T>
concept bool RangeForAble = requires (T t) {
   requires requires (decltype(begin(t)) b, decltype(end(t)) e) {
     b != e;
     ++b;
     *b;
   };
 };

int main()
{
static_assert(RangeForAble<std::vector<int>>);
static_assert(RangeForAble<double>);
}
0

In C++20 it will look something like this:

template< class T >
concept RealContainer = requires(T&& t) {
  std::begin(std::forward<T>(t));
  std::end  (std::forward<T>(t));
};

Probably not perfect, but works for std::vector and C array, demo: https://gcc.godbolt.org/z/M4xhnqG46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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