I'm struggling to satisfy a C++20 concept for my class.
struct C1
{
int* begin();
int* end();
};
static_assert(std::ranges::range<C1>); // Compiles fine
struct C2 {};
namespace std::ranges // (A)
{
auto begin(C2&);
auto end(C2&);
}
static_assert(std::ranges::range<C2>); // Fails to compile
The std::ranges::range<>
concept is defined as follows:
template <class _Rng>
concept range = requires(_Rng& __r) {
std::ranges::begin(__r);
std::ranges::end(__r);
};
Taking this literally instructs me to declare the functions (A) above.
But these doen't help.
On the other hand, defining the two member functions as in class C1
does work.
My problem is that I just don't see the semantic connection between the requirement imposed by the concept and the solution that helps for class C1
. It tells me to do one thing when in fact I need to do another thing.
Please provide some explanation for this.
Background: In my production code, my class does define a begin()
and end()
function that return an iterator each. But the range
concept is not satisfied with these definitions and fails to show me why.
See the full example on Compiler Explorer