Thursday, 15 May 2014

c++ - Member function pointer wrapper using variadic template -



c++ - Member function pointer wrapper using variadic template -

i'm trying compile code using visual c++ 2013 , want take advantage of variadic templates. have several classes wrap function pointer - several versions different number of arguments.

the wrapper fellow member function 1 argument following:

template <typename t, t> struct proxy; template <typename t, typename r, typename arg0, r(t::*mf)(arg0)> struct proxy<r(t::*)(arg0), mf> { proxy(t& host) : m_host(host) {} template <typename arg0> r call(arg0&& arg0) { homecoming (m_host.*mf)(std::forward<arg0>(arg0)); } private: proxy& operator=(const proxy&); t& m_host; };

let's have test class:

class somehost { public: int somegetter() { homecoming 42; } void somesetter(int var) { m_var = var;} private: int m_var; };

and test case:

void test() { somehost obj; proxy<void(somehost::*)(int), &somehost::somesetter> g(obj); g.call(5); }

everything works fine far. rewrote proxy class using variadic template:

template <typename t, typename r, typename... args, r(t::*mf)(args...)> struct proxy<r(t::*)(args...), mf> { proxy(t& host) : m_host(host) {} template <typename... args> r call(args&&... args) { homecoming (m_host.*mf)(std::forward<args>(args)...); } private: proxy& operator=(const proxy&); t& m_host; };

using variadic template, visual c++ 2013 shows me several compiler errors coming test function:

file.cpp(79): error c2440: 'specialization' : cannot convert 'overloaded-function' 'void (__thiscall somehost::* )(void)' 1> none of functions name in scope match target type: see reference class template instantiation 'proxy<void (__thiscall somehost::* )(int),somehost::somesetter>' beingness compiled 1>file.cpp(79): error c2973: 'proxy<r(__thiscall t::* )(args...),mf>' : invalid template argument 'overloaded-function' 1>file.cpp(40) : see declaration of 'proxy<r(__thiscall t::* )(args...),mf>'

i tested template somehost::somegetter() , worked without problems.

any help highly appreciated.

thanks lot...

c++ templates c++11 variadic-templates member-function-pointers

No comments:

Post a Comment