I have this strange (for me) behavior which I'm not able to explain and I wish to have your comments on this, please have a look at the following code:
void detach(callback_function_ptr const& ptr) {
observers.erase(remove_if(
observers.begin(),
observers.end(),
[&ptr](callback_function_ptr const& ptr2) {
return ptr.target<void(worker*)>() == ptr2.target<void(worker*)>();
}
));
}
When calling 'target' I need to specify which on I want to use, otherwise I get this compilation error:
test.cpp:47:44: error: no matching function for call to 'std::function<void(worker*)>::target() const'
And this is somehow understandable to me, I thought I may avoid to specify each time which 'target' function I want by preparing such a small 'helper':
template<typename T>
worker_ptr get_target(const function<T>& callback) {
return callback.target<T>();
}
Which looks a reasonable solution (to me :) ), but doesnt work:
test.cpp: In member function 'void (worker::* worker::get_target(const std::func
tion<T>&))(worker*)':
test.cpp:18:27: error: expected primary-expression before '>' token
return callback.target<T>();
^
test.cpp:18:29: error: expected primary-expression before ')' token
return callback.target<T>();
^
Well, here I don't catch the problem, why is not valid? Even if I use void(worker*) instead of T a compilation error is still present:
test.cpp:18:26: error: expected primary-expression before 'void'
return callback.target<void(worker*)>();
^
test.cpp:18:26: error: expected ';' before 'void'
test.cpp:18:26: error: expected primary-expression before 'void'
test.cpp:18:26: error: expected ';' before 'void'
I had used void(worker*) before in the 'detach' function with no problems, why here is not working? How does exactly behave the template type deduction and function call resolution in this case?
Thanks for your help