开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜