如果您有一個指向成員函式的指標,如下所示:
struct Foo { void func() {} };
void(Foo::*funcPtr)() = &Foo::func;
有沒有辦法在Foo::洗掉后獲取函式的型別?
IE,
void(Foo::*)() -> void(*)()
int(Foo::*)(int, double, float) -> int(*)(int, double, float)
你明白了。
目標是讓std::function接受一個像這樣的函子:
struct Functor { void operator()(...){} }
Functor f;
std::function< magic_get_function_type< decltype(Functor::operator()) >::type > stdfunc{f};
是否可以?
uj5u.com熱心網友回復:
要回答您的問題,可以使用一個簡單的模板:
template <typename Return, typename Class, typename... Args>
Return(*GetSig(Return(Class::*)(Args...)))(Args...);
這定義了一個被呼叫的函式GetSig,該函式將成員函式指標作為引數,并從本質上提取Return型別和Args...,并將其作為非成員函式指標回傳。
用法示例:
class C;
int main() {
using FuncType = int(C::*)(double, float);
FuncType member_pointer;
decltype(GetSigType(member_pointer)) new_pointer;
// new_pointer is now of type int(*)(double, float) instead of
// int(C::*)(double, float)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/383766.html
上一篇:使用Python用戶輸入更新值
下一篇:c語言中的陣列可視化函式
