Below I have a template function named ProxyCall, which accepts an object, a member function and its arguments. It simply forwards the call to the member function.
I would like to be able to call the function without using template qualifiers (imagine tons of such calls with multiple arguments). The type deduction mostly works but the compilers (both msvc and gcc 4.9) barf when I try to pass const reference parameters as in the example.
#include <string>
struct Widget {
void f(const std::string& s, bool b) {}
};
template<typename T, typename... Args>
void ProxyCall(T &obj, void(T::*method)(Args...), Args&&... args) {
(obj.*method)(std::forward<Args>(args)...);
}
int main(int argc, char* argv[])
{
Widget w;
std::string s;
ProxyCall<Widget, const std::string&, bool>(w, &Widget::f, s, true); // OK
ProxyCall(w, &Widget::f, (const std::string&)s, true); // also OK
ProxyCall(w, &Widget::f, s, true); // ERROR: template parameter is ambiguous
return 0;
}
My question is: How can I modify the above code so that the compiler would automatically deduce the types without resorting to explicit template qualifiers or explicit casting. It seems this should be possible considering that the compiler already knows that exact arguments types from the signature of Widget::f.