繁体   English   中英

模板参数指向方法的指针

[英]Pointer to method from template arguments

我有一个问题,我不知道如何从模板参数创建指向方法的指针

 /* 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不是问题。 无论是使用using或放置在一个类:

template < typename controlledListType >
using ptrMethod  = void (ControlingComponent::*) ( std::list < std::weak_ptr< controlledListType > >,
                                                   std::weak_ptr< controlledListType >,
                                                   nlohmann::json );

要么

template < typename controlledListType >
struct some_name
{
    typedef void ( ControlingComponent::*ptrMethod ) ( std::list < std::weak_ptr< controlledListType > >,
                                                       std::weak_ptr< controlledListType >,
                                                       nlohmann::json );
};

您不能将typedef与模板一起使用,该语言不支持。 您可以做的是使用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 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM