'Template of multiple abstract classes

There are many questions about template specialization with abstract classes, still I couldn't find something that helped me solve my problem.

First of all, let's say that we have three abstract classes: A, B and C. Both B and C are derived from A:

class A {
    virtual void do_something();
};

class B : public A {
    virtual void do_something_else();
};

class C : public A {
    virtual void do_something_different();
};

Now, let's say that I have a templated class that should take as template parameters classes derived from A. I can implement this constraint using C++11 std::is_base_of. This class has a method execute. What I want to accomplish is this: is the template parameter is also derived from B, this method should have only one parameter, if it's derived from C, the method should have two parameters.

How do I do that? Is it even possible?



Solution 1:[1]

C++20 way:

#include <concepts>
template<typename ty>
struct foo{ 
    return_type bar() requires std::drived_from<ty,A>;
    return_type bar(arg_type1) requires std::drived_from<ty,B>;
    return_type bar(arg_type1,arg_type2) requires std::drived_from<ty,C>;
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1