开发者

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

开发者 https://www.devze.com 2023-01-23 19:59 出处:网络
The following code d开发者_如何学Pythonoes not compile ... any idea why? Is this illegal C++? class Handler {

The following code d开发者_如何学Pythonoes not compile ... any idea why? Is this illegal 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:

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

Because the template handle is qualified with a dependent type. (Just like you use typename if a type is qualified with a dependent type.)

0

精彩评论

暂无评论...
验证码 换一张
取 消