I have an abstract base class and then a templated derived class, for certain types (specified by a template), I want to implement the virtual base methods in the derived class, for others, this should be left open (with the need for a further derivation):
#include <iostream>
struct cat {
void meow() {
std::cout << "Meow!\n";
}
};
struct base {
virtual void foo_base() = 0;
};
template<typename T>
concept can_meow = requires(T t){t.meow();};
template<class T>
struct derived: public base {
derived() requires can_meow<T> {
std::cout << "Wrapper constructor with meow!\n";
}
derived() requires (!can_meow<T>) {
std::cout << "Wrapper constructor w/o meow!\n";
}
void foo_base() requires can_meow<T> final {};
};
template<class T>
struct derived2: public derived<T> {
void foo_base() final {};
};
int main() {
derived<cat> c; //works
//derived2<int> i; //error
return 0;
}
How do I get this to work? The error I'm currently getting is:
<source>:28:6: error: use of function 'void wrapper<T>::foo_base() requires can_meow<T> [with T = int]' with unsatisfied constraints
Cheers and thanks
Update: godbolt https://godbolt.org/z/T4v7Yjq5f
You are not allowed to constrain a virtual function in any way (See: [class.virtual]p6).
You can achieve a similar effect by having a conditional base class that could override the function. For example: