I'm trying to implement CRTP with C++20 concept. The key is to prevent the parent class template from being instantiated by any type argument except its subclass.
Compiler complains about the self-referencing CRTP_Root in the requires-clause:
#include <concepts>
//error: ‘CRTP_Root’ was not declared in this scope
// | requires std::derived_from<derived_type, CRTP_Root<derived_type>>
template<typename derived_type>
requires std::derived_from<derived_type, CRTP_Root<derived_type>>
class CRTP_Root {
};
class CRTP_child : public CRTP_Root<CRTP_child> {
};
Since requires-clause must be part of the declaration, there seems no way to write a forward declaration of such self-referring constrained class template. Is there any workaround?