c++templatesc++20grammar

Template Circulation Definition


I am creating an intrusive pointer as follows:

class RefCounted {};

template <typename T>
concept RefCountedDerived = std::is_base_of_v<RefCounted, T>;

template <RefCountedDerived T>
class IntrusivePtr {};

When I perform a test with it, like:

class Test : public RefCounted
{
    static IntrusivePtr<Test> create();
}

The compiler complains that the class Test is not defined and cannot be the argument of __is_base_of. I assume that when the compiler reads the static function, it resolves to RefCountedDerived, but the class Test has not completed its definition. Actually, removing the static function resolves the issue.

So is there any solution to solve it without removing that static function?


Solution

  • is there any solution to solve it without removing that static function?

    Yes, make it auto:

    class Test : public RefCounted {
        inline static auto create();
    };
    
    // definition in header:
    auto Test::create() {
        return IntrusivePtr<Test>{};
    }