簡體   English   中英

如何將模板參數中的函數或成員函數指針轉換為函數類?

[英]How to convert a function or member function pointer in template arguments to a function class?

如何將模板參數中的函數或成員函數指針轉換為函數類? 假設函數類是定義成員函數operator()

我認為將函數和成員函數的使用歸納在模板中很有用。

例如,

template<..., class FunClass>
class MyClass {};

然后,可以根據MyClass的要求從各種函數指針,具有不同數量參數的成員函數指針轉換FunClass。 有任何簡單的轉換方法嗎? 謝謝。

按照注釋中的討論進行操作:您可以通過以下方式提取ConvertToFunClass :(從此處被盜)

template<typename Sig>
struct Types;

template<typename RetType, typename ClassType, typename Arg0>
struct Types<RetType (ClassType::*)(Arg0)>
{
    typedef RetType return_type;
    typedef ClassType class_type;
    typedef Arg0 argument_0;
};

用法可以是MyClass<T, R, Types<decltype(&MyFun)>::ClassType > a;

(免責聲明:我沒有嘗試過代碼,無法訪問編譯器ATM。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM