简体   繁体   English

如何从模板类型获取指向模板化成员函数的指针?

[英]How can you get pointer to a templated member function from a template type?

The following code does not compile ... any idea why? 以下代码无法编译......任何想法为什么? Is this illegal C++? 这是非法的C ++吗?

class Handler {
 public:
  template <typename T>
  void handle(T t) {}    
};

class Initializer {
 public:
  template <typename T, typename H>
  void setup(H *handler) {
    void (H::*handle)(T) = &H::handle<T>; // fails
  }
};

int main() {
  Initializer initializer;
  Handler handler;
  initializer.setup<int, Handler>(&handler);
}

You need template : 你需要template

void (H::*handle)(T) = &H::template handle<T>; 

Because the template handle is qualified with a dependent type. 因为模板handle是使用依赖类型限定的。 (Just like you use typename if a type is qualified with a dependent type.) (就像你使用typename如果一个类型用依赖类型限定。)

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

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