I’m trying to implement a very example using CRTP + concepts.
First, here’s the code:
#include <concepts>
template<typename T>
concept HasOnEvent = requires(T* t)
{
{ t->on_event() } -> std::same_as<void>;
};
template<HasOnEvent Deriv>
struct Base
{
void call_on_event()
{
static_cast<Deriv*>(this)->on_event();
}
};
struct Derived : Base<Derived>
{
void on_event()
{
}
};
int main()
{
Derived d;
d.call_on_event();
}
I’m using clang 18 with -std=c++23.
I feel like I’m using concepts correctly, but then I get errors. Sifting through them, I think here’s the main issue:
't->on_event()' would be invalid: member access into incomplete type 'Derived'
Isn’t this the whole point of CRTP (to not define the type beforehand)?
Can someone help me to get this simple example working?
Thank you!