I am trying to get the following simple code to work:
#include <functional>
#include <iostream>
class Parent {
public:
void method(int a) {
std::cout << "Parent::method(" << a << ")" << std::endl;
}
};
class Child : public Parent {
public:
};
template <typename T>
std::function<void()> test(T* v, void(T::* fn)(int)) {
return std::function<void()>([v, fn](){
v->fn(10);
});
}
int main() {
Child c;
std::function<void()> fn = test(&c, &Child::method);
fn();
return 1;
}
But am getting the following error:
note: template argument deduction/substitution failed:
note: deduced conflicting types for parameter 'T' ('Child' and 'Parent')
std::function<void()> fn = test(&c, &Child::method);
I get the same problem if I change my pointer to Parent::method. Since Child "is a" parent, why is the compiler unable to deduce this template?
Thanks!