For the following type system,
template <typename U, typename V>
struct Base { ... };
struct Derived1 : Base<TypeA, TypeB> { ... };
struct Derived2 : Base<TypeA, TypeC> { ... };
I would like to be able to write functions that accept parameters that have been derived from Base
while restricting some (none, all) of the template parameters in Base
. My idea is to use C++ concepts for this (though I’m open for other approaches!). Essentially, I am looking for something like
template <typename T, typename U, typename V>
concept FromBase = requires ...
such that I can write functions like this
void foo(FromBase auto param); // accepts anything derived from `Base`
void bar(FromBase<TypeA> auto param); // accepts `Derived1` and `Derived2`
void baz(FromBase<TypeA, TypeB> auto param); // accepts `Derived1`, but not `Derived2`
How would I do this with concepts? Or is this even possible with C++? Note that I’m specifically looking for a solution that is “easy on the eyes and mind”, i.e., ideally I don’t want to use N different concepts to support N potential parameters.
Bonus question: Is this also possible with mixed type/non-type template parameters in Base
?