Sorry for the ambiguous title, I couldn't find the right words to phrase it.
I'm trying to force a class to define some functions/operators to be correct. for example ForwardIterator
is required to have some operators otherwise I should get a compiler error. I have asked the question here and I was given a method with inheritance, it works perfectly but C++20 concepts seem to be more intuitive and give better errors.
The initial idea is:
template <typename T> concept is_ForwardIterator= requires(T x, T y)
{
x == y;
};
template <typename T>
requires is_ForwardIterator<T>
struct ForwardIterator {
};
Then whenever I want to implement a forward iterator I inherit from ForwardIterator
:
struct MyCustomIterator
: public ForwardIterator <MyCustomIterator>
{
bool operator ==(const MyCustomIterator& other) const;
};
But the compiler keeps complaining that MyCustomIterator
doesn't satisfy the requirement:
error: constraints not satisfied for class template 'ForwardIterator' [with T = MyCustomIterator]
note: because 'MyCustomIterator' does not satisfy 'is_ForwardIterator'
template <typename T> requires is_ForwardIterator<T> struct ForwardIterator {
^
note: because 'x == y' would be invalid: invalid operands to binary expression ('MyCustomIterator' and 'MyCustomIterator')
x == y;