c++ - 指向模板类方法的指针的 Typedef

标签 c++ templates c++11 typedef

标题总结了我的问题 - 我需要一个通用的 typedef 作为指向模板类方法的指针,如下面的代码所述。 typedef 需要是通用的。

template<typename TYPE>
struct MyClass {
    const TYPE& get() const {}
};

// this is okay:
typedef void (MyClass<int>::*ParticleMethodPtr)( int );

// now I just need to typedef this so I can
// use the pointer on methods other than int

// using typedef, not okay:
template< TYPE >
typedef void (MyClass<TYPE>::*ParticleMethodPtr)( TYPE );

最佳答案

在 C++11 中:

template<typename TYPE>
using ParticleMethodPtr = const TYPE&(MyClass<TYPE>::*)() const;

ParticleMethodPtr<int> p = &MyClass<int>::get;

关于c++ - 指向模板类方法的指针的 Typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16007153/

相关文章:

c++ - 为什么这个程序输出相同的结果 4 次?

c++ - 如何使用map通过指向函数的指针构造对象

c++ - 实现简化的 C++ vector 类拷贝 - 无法编译

c++ - 转置模板参数包

c++ - 如果我在返回后声明它,存储数组在哪里?

带有 C++ UnsatisfiedLinkError 的 Android Studio JNI

c++ - 使用 C++ 和程序集在运行时分配和创建新函数

c++ - 自定义分配器兼容性

c++ - 将指针作为参数传递给 std::thread 函数 C++11

c++ - Xcode 4.2 无法识别 C++ 原始字符串文字?