Checking for member functions using concepts

Viewed 24

I am trying to check if a given type has a member function using concepts, to implement a CRTP interface.

The following code compiles:

template <typename T>
concept HasLoop = requires(T t) {
    t.Loop();
};

template <HasLoop T>
void fn(T arg)
{
    arg.Loop();
}

struct Obj
{
    void Loop()
    {
        
    }
};

int main()
{
    Obj o;
    fn(o);
    return 0;
}

but this here doesn't compile:

template <typename T>
concept HasLoop = requires(T t) {
    t.Loop();
};

template <HasLoop l>
struct StaticInf
{

    void StartLoop(void* ptr)
    {
       static_cast<l*>(ptr)->Loop();
    }
};

struct Obj : StaticInf<Obj>
{
    void Loop()
    {
        
    }
};

int main(void)
{
    Obj o;
    return 0;
}

So, the question is: why does it compile with templated functions and not with the CRTP?

0 Answers
Related