Trying to get compile time method and object selection without base class and virtual calls.
Here is the case:
struct A {
    void f1()const { cout << "A::f1" << endl;}
    void f2()const { cout << "A::f2" << endl;}
};
struct B {
    void f1()const { cout << "B::f1" << endl;}
    void f2()const { cout << "B::f2" << endl;}
};
class Holder {
    A* _a = nullptr;
    B* _b = nullptr;
public:
    Holder(A* a): _a(a) {}
    Holder(B* b): _b(b) {}
    void f1()const {
        if(_a)       _a->f1();
        else if(_b)  _b->f1();
    }
    void f2()const {
        if(_a)       _a->f2();
        else if(_b)  _b->f2();
    }
};
void f(const Holder& h) {
    h.f1();
}
int main() {
    B obj;
    Holder h(&obj);
    f(h);
}
http://coliru.stacked-crooked.com/a/4b5acec6866cfd4e
Suppose there are very few classes like A and B but there may be a lot of functions like f1 and f2.
Holder needs to call function on the actual object that it holds, without polymorphism and without a need for inheritance / shared interface for A and B.
Looking for a nice way to do something like:
class Holder {
    A* _a = nullptr;
    B* _b = nullptr;
public:
    Holder(A* a): _a(a) {}
    Holder(B* b): _b(b) {}
    // below is pseudo code!
    void call<function>()const {
        if(_a)
            _a->function(); // function is known in compile time, sort of...
        else if(_b)
            _b->function();
    }
    void f1()const { call<f1>(); }
    void f2()const { call<f2>(); }
};
Any idea?
- macro?
- template?
- other trick?
 
    