Am 11.05.2012 21:21, schrieb Marc:
> Hello,
>
> I have several overloads of a function, I have a set of arguments,
> and I would like to get a function pointer to the overload the
> compiler would pick if I called this function on these arguments. I
> am not interested in applying the function now, I just want to get
> that pointer.
>
> Is this possible? Or is anything vaguely similar possible?
>
>
"Something similar" is possible:
http://stackoverflow.com/a/10398323/34509
However it does not take a set of arguments, but a set of parameters
of the function you are interested in. Summary:
-----------------------
template<typename T>
struct identity { typedef T type; };
template<typename T>
using NoDeduce = typename identity<T>::type;
template<typename T>
using Identity = T;
template<typename ...P, typename R>
Identity<R(P...)> *get_f(R f(NoDeduce<P>...)) {
return f;
}
// R f(int, int, int);
auto *f1 = get_f<int, int, int>(f);
// R f(int, int);
auto *f2 = get_f<int, int>(f);
--------------------
If you have a set of (returntype, parameters) pairs and a single
argument tuple and wanna see what parameter tuple the compiler would
take (perhaps for teaching purposes), you could produce a set of
surrogate call candidates like this
template<typename T>
struct identity { typedef T type; };
template<typename T>
using Identity = typename identity<T>::type;
template<typename ...P>
struct F { };
template<typename R, typename ...P1, typename ...P>
struct F<R(P1...), P...> : F<P...> {
operator Identity<identity<R(P1...)>(P1...)>*() const {
throw "You shall not call me";
}
};
F<void(int&), void(int&&)> f;
static_assert(
is_same<decltype(f(10))::type, void(int&&)>::value,
"something gone wrong");
With a bit more effort, you could use real member functions and make
them return a function pointer of your choice.
Hope it helps.