受到另一个问题的启发,我试图找到一种方法来推断给定用于调用该函数的实际参数的重载成员函数的类型。这是我到目前为止所拥有的:
#include <type_traits>
template<typename F, typename Arg>
struct mem_fun_type {
// perform overload resolution here
typedef decltype(std::declval<F>()(std::declval<Arg>())) result_type;
typedef decltype(static_cast<result_type (F::*)(Arg)>(&F::operator())) type;
};
struct foo {};
struct takes_two
{
void operator()(int);
void operator()(foo);
};
struct take_one {
void operator()(float);
};
int main()
{
static_assert(std::is_same<mem_fun_type<take_one, float>::type,
void (take_one::*)(float)>::value, "Zonk");
static_assert(std::is_same<mem_fun_type<takes_two, double>::type,
void (takes_two::*)(float)>::value, "Zonk");
return 0;
}
只要模板参数 Arg 与实际类型匹配,static_cast 就会成功,但这只是重载解析(精确匹配)最简单的情况。是否可以在模板元编程中执行完整的重载解决过程?
这纯粹是假设性的,不适合实际使用。