[英]Function pointer on static method in template class
我有以下代碼在其中初始化函數指針表。 解析輸入文件時使用該表。
class TorchModule { ... };
class TorchLinear : public TorchModule { ... };
class TorchView : public TorchModule { ... };
...
typedef std::shared_ptr<const TorchModule> ( *load_function )( File* file );
using table_type = std::map< std::string, load_function > table_type;
table_type load_Object = {
{"nn.Linear", &TorchLinear::load },
{"nn.View" , &TorchView ::load }
};
如果基類TorchModule是模板類,如何更新代碼。
template<MODE mode>
class TorchModule { ... };
template<MODE mode>
class TorchLinear : public TorchModule<mode> { ... };
template<MODE mode>
class TorchView : public TorchModule<mode> { ... };
您可以在模板類中將表定義為靜態變量。
template<MODE mode>
using load_function = std::shared_ptr< const TorchModule<mode> > (*)( File* file );
template<MODE mode>
using table_type = std::map< std::string, load_function<mode> >;
template<MODE mode>
struct Table {
static table_type<mode> table;
};
template<MODE mode>
table_type<mode> Table<mode>::table = {
{"nn.Linear", &TorchLinear<mode>::load },
{"nn.View" , &TorchView<mode> ::load }
};
筆記:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.