c++ - 从模板参数指向方法的指针

标签 c++ templates pointers

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

 /* 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/

相关文章:

java - 非静态二叉树的紧凑存储

c++ - 全阵列替代

c++ - 未使用的变量 'class' [-Wunused-variable] - 错误/警告

iphone - 如何使用 Xcode4 项目模板将文件添加到复制捆绑资源构建阶段

c - 使用 valgrind 和 gdb 进行调试

c - 打印 (Char*)(Void*) 在主程序中有效,但在函数中无效

c++ - 如何使我的 C++ 代码与 QT 按钮一起工作并打印到 QlineEdit?

c++ - VS2010 C++成员模板函数特化错误

templates - 如何在 ASP.NET Core 2.1 中向 Visual Studio 添加新的 Razor 页面模板?

C - "char var[]"和 "char *var"之间的区别?