1

This is a continuation of the following SO question c++11 get type of first (second, etc...) argument, similar to result_of.

I need to get the type of the (first) argument of a class function. The additional problem is that the function is overloaded, and that the argument type may be a (pointer to) a base class of a known type. I first need to disambiguate the call using this second, known, type. See code example.

class Abase {
public:
    void fun2() {
        // Success!
    }
};
class C {void fun2() {};};
class A1 : public Abase {};
class A2 : public Abase {};
class A : public A1, public A2 {};


class B {
public:
    void fun1(A1* t) {}
    void fun1(C* t) {}
};

template <typename T1,typename T2>
void problematic(T1* a, T2* b)
{
    //This is ambiguous due to inheritance
    a->fun2();
    // I want to achieve this
    static_cast<A1*>(a)->fun2();
    // Based on type of first argument of (the overloaded) fun1 actually being called
    b->fun1(a);
    static_cast</*INSERT METAPROGRAMMING HERE*/>(a)->fun2();
}
int main()
{
    A a;
    B b;
    problematic(&a, &b);
}

The following would have solved the problem if not for the overloaded fun1

template<class T>
// Does not work. fun1 is ambiguous due to overloaded function
struct function_traits : function_traits<decltype(&T::fun1)> {
};
template<class T, class... Args>
struct function_traits<void(T::*)(Args...)> {
    using argument_types = std::tuple<Args...>;
};

template<class T>
using first_argument_type = typename std::tuple_element<0, typename function_traits<T>::argument_types>::type;

// ...

// :-(
static_cast<first_argument_type<T2>>(a)->fun2();
John
  • 145
  • 1
  • 9

0 Answers0