我有一个问题,我不知道如何从模板参数创建指向方法的指针
/* Pointer to function */
template < typename controlledListType >
typedef void ( ControlingComponent::*ptrMethod ) ( std::list < std::weak_ptr< controlledListType > >,
std::weak_ptr< controlledListType >,
nlohmann::json );
/* Function to add a component which will be controlled */
void addComponent ( std::weak_ptr< Component > _wp, ptrMethod _ptr );
错误:
error: template declaration of ‘typedef’ in
typedef void ( ControlingComponent::*ptrMethod ) ( std::list < std::weak_ptr< controlledListType > >
error: ‘ptrMethod’ has not been declared
void addComponent ( std::weak_ptr< Component > _wp, ptrMethod _ptr );
有人知道我的问题是怎么解决的吗?
最佳答案
您不能将 typedef
与模板一起使用,该语言不支持。你可以做的是添加一个 alias declaration使用 using
语句,如
template < typename controlledListType >
using ptrMethod = void (ControlingComponent::*) ( std::list < std::weak_ptr< controlledListType > >,
std::weak_ptr< controlledListType >,
nlohmann::json );
然后你就可以像这样使用它了
void addComponent ( std::weak_ptr< Component > _wp, ptrMethod<some_type> _ptr );
关于c++ - 从模板参数指向方法的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53482601/