for my test about CRTP I created this base class:
template<typename Derived>
struct Base
{
void print() const { std::cout << "print\n"; }
};
And this class:
struct A: public Base<A>
{
void printSub() { std::cout << "printSub from A\n"; }
};
And I can use these classes without problem:
A a1{};
a1.print();
a1.printSub();
My problem is: how is possible to use concept/requires for template class?
I want add something like this:
template<typename Derived> requires requires (Derived t) {
t.printSub();
}
struct Base
{
void print() { std::cout << "print\n"; }
};
To accept only Derived class with printSub method. But I receive this error:
template constraint failure...
I did some tests but I did not find solutions (I also found this thread but it can't help me).
Where am I wrong? Thanks for any help
CodePudding user response:
You cannot meaningfully constrain the derived class template parameters of a CRTP base class for the same reason that you can't do this:
template<typename Derived>
class Base
{
using alias = Derived::SomeAlias;
};
Derived
isn't complete yet, and using a constraint usually requires completeness.