請參考以下內容:
struct functorOverloaded
{
void operator()(const int& in_, ...) const {}
void operator()(short in_) {}
};
// helper to resolve pointer to overloaded function
template <typename C, typename... OverloadArgs>
auto resolve_overload(
std::invoke_result_t<C, OverloadArgs...> (C::* func)(OverloadArgs..., ...) const
)
{ return func; };
int main(int argc, char **argv)
{
using C = const functorOverloaded;
// works with exact function type
using myT = decltype(resolve_overload<C, const int&>(&C::operator()));
// can call with something convertible to const int&
static_assert(std::is_invocable_v<C,int>, "!!!");
// how to get the pointer to the overload that would be called when passed int (or double)?
// the next line doesn't compile (error C2672: 'resolve_overload': no matching overloaded function found)
using myT2 = decltype(resolve_overload<C, int>(&C::operator()));
return 0;
}
上面的代碼允許檢索指向函式的特定多載的指標(operator()在這種情況下),請參見此處。在這種情況下,必須知道確切的引數型別 ( const int&) 才能獲取指標,即使我可以只使用普通的int,甚至. 來呼叫特定的多載double。是否可以獲得指向將使用特定引數呼叫的多載的指標(假設呼叫是可決議的/不模棱兩可的)?
編輯:添加背景關系:
我正在撰寫一個用于內省可呼叫物件的invocable_traits庫。例如,給定一個 Callable,它會告訴您回傳型別、arity 和引數型別以及其他一些屬性。為了支持具有多載(或模板化)的函子(包括 lambdas)operator(),APIinvocable_traits允許指定呼叫引數以消除要使用的多載(或實體化模板)的歧義。但是,必須知道確切的引數型別(const int&在上面的示例中),在這種情況下簡單地指定int是行不通的,因為沒有帶有簽名的函式R operator()(int). 理想情況下,我希望允許發現在給定輸入引數型別的情況下呼叫的確切多載/實體化的簽名,理想情況下甚至考慮應用的任何隱式轉換。這可能嗎?
uj5u.com熱心網友回復:
除非您已經知道它的簽名,否則無法獲取將使用給定引數呼叫的多載集的函式。
如果你知道,那有什么意義呢?
問題在于,對于任何給定的引數,考慮到隱式轉換、參考、cv 限定符、、noexcept舊式可變引數、默認引數,以及可能還有作為空指標常量的字面量 0,存在無限數量的函式簽名這將匹配。目前還沒有“僅僅”列出所有候選人的設施。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/415749.html
標籤:
