3
受另一個問題的啓發,我試圖找到一種方法來推導出一個重載成員函數的類型 ,給出用於調用 函數的實際參數。以下是我迄今爲止:使用模板元編程執行重載解析
#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;
}
只要模板參數精氨酸的實際類型相匹配的 的static_cast會成功,但是這僅僅是 重載解析(精確匹配)的最簡單的情況。是否有可能在模板元編程中執行完整的重載解析過程 ?
這是純粹的假設,不適合現實世界的使用。
這是一個純粹的現實生活中的例子,昨天我用'boost :: bind-vs-std :: bind'發佈了這個問題! – vines
我回答了你提到的[這裏]的問題(http://stackoverflow.com/a/8938409/147192)。沒有客戶的幫助,我只是看不出有關重載解決方案的理由。 –
你是一個貪婪的懲罰者。 –