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

The following code does not compile ... any idea why? Is it 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);
}
+3
source share
1 answer

You need to template:

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

Because the pattern handlematches the dependent type. (Just like you use typenameif the type matches the dependent type.)

+3
source

Source: https://habr.com/ru/post/1772787/