I am trying to construct a virtual equal operator, could you please help? equalable is a class such that all inherting classes require the is_equal functions between type(*this).
When I delete the requiere it compiles, but I didn't test to see if the code is correct, just trying to make it compile (Error is at the end of the code):
#include <iostream>
#include <concepts>
#include <memory>
template<class T>
concept has_is_equal = requires(T x, T y)
{
{ x.is_equal(y) } ->std::same_as<bool>;
};
template <class T>
requires has_is_equal<T>
class equalable
{
public:
using derived_type = T;
template<typename U>
bool operator==(const U& other) const
{
return this->equal<U>(other);
}
private:
template<class U>
bool equal(const equalable<U>& other) const
{
return false;
};
template<>
bool equal<T>(const equalable<T>& other) const
{
const T& c_other = static_cast<const T&>(other);
const T& c_this = static_cast<const T&>(*this);
return c_this.is_equal(c_other);
};
};
class derived_one : public equalable<derived_one>
{
public:
int a = 1;
bool is_equal(const derived_one& other) const
{
return a == other.a;
}
};
class derived_two : public equalable<derived_two>
{
public:
int a = 2;
bool is_equal(const derived_two& other) const
{
return a == other.a;
}
};
int main()
{
derived_one one; one.a = 1;
derived_one two; two.a = 2;
bool alpha = (one == two);
std::cout << alpha;
}
Error message is , I am using VS with this
Could you please help?
Thanks very much!