I am sure this can be done since it can probably done with a c macro, but can't wrap my head around the syntax. or if it's even possible.
Say I have class B and class C, both implementing function foo(int), both inheriting A, but when I call my helper I don't want to know the type(say I call with enum type, something like this:
I was thinking along the way of, but can't get the final touch:
#include <iostream>
#include <memory>
using namespace std;
struct A {};
template< typename R, typename C, typename... Args > R member_func(R (C::*)(Args...));
template<typename Child_t, typename Child_t::* func, typename ...Args_t>
auto activate(A& parent, Args_t... args) -> decltype(member_func(Child_t::func)) { return static_cast<Child_t&>(parent).*func(forward<Args_t>(args)...); }
template<typename (???)::* func, typename ...Args_t>
auto activate(int type, A& parent, Args_t... args) -> decltype(??? they would both return the same thing) {
switch (type) {
case 0: return activate<B>(parent, forward<Args_t>(args)...);
case 1: return activate<C>(parent, forward<Args_t>(args)...);
}
}
struct B : public A { int test(int i) { return i; } };
struct C : public A { int test(int i) { return i+1; } };
void foo() {
int type = 0;
A* child = new B;
cout << activate(type, *child, ??test??, 4) << endl;
}
Is it even possible? Hints? taking suggestions for question name :)
Aucun commentaire:
Enregistrer un commentaire