Below I have a template function called ProxyCall that takes an object, a member function and its arguments. It simply redirects the member function call.
I would like to be able to call a function without using qualifier templates (imagine tons of such calls with multiple arguments). Type inference basically works, but the compilers (both msvc and gcc 4.9) are 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 change the above code so that the compiler automatically infers types without resorting to explicit template selections or explicit casting. This seems to be possible, given that the compiler already knows that the exact argument types are from the Widget :: f signature.
source share