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.)
精彩评论