c++ - Correct function signature template for member function -
following on question asked, there might unnecessary stuff in there example small, want (and of course if can think of other cool ways this, please share thoughts), allow user activate non virtual non interface correlating child method using specific type (please, let's concentrate on how , not why :) ).
my last error concerns member function signature , not selection actually, can't figure how allow perfect forwarding, i've tried current solution can never work(copies), , i've tried transfer args_t&& , forward, did not work either, suggestions on how transfer member function correctly? suspect activate function defenition wrong one...
i've added code demonstrates compilation error, can change activate args_t input parameter args_t&& , forward(args)... see second...
thanks.
struct type { enum value { one, 2 }; }; struct {}; template<typename type_t, typename r, typename... args_t> auto activate(r (type_t::* f)(args_t...), a& parent, args_t... args) -> r // args&& won't comppile either.. { return (static_cast<type_t&>(parent).*f)(args...); } template<typename type_t, typename r, typename... args_t> auto activate(r (type_t::* f)(args_t...) const, const& parent, args_t... args) -> r { return (static_cast<type_t const&>(parent).*f)(args...); } struct noncopyable { public: noncopyable() {} private: noncopyable(noncopyable const& other) {} }; struct b : public { noncopyable& foo(noncopyable& other, bool test) { return other; } }; struct c : public { noncopyable& foo(noncopyable& other, bool test) { return other; } }; // else obviously... #define funcselect0(type, parent, func) \ type == type::one? activate<b>(&b::func, parent) : \ activate<c>(&c::func, parent) #define funcselect1(type, parent, func, arg1) \ type == type::one? activate<b>(&b::func, parent, arg1) : \ activate<c>(&c::func, parent, arg1) #define funcselect2(type, parent, func, arg1, arg2) \ type == type::one? activate<b>(&b::func, parent, arg1, arg2) : \ activate<c>(&c::func, parent, arg1, arg2) #define get_fs(_1,_2, _3, _4, _5, name,...) name #define funcselect(...) (get_fs(__va_args__, funcselect2, funcselect1, funcselect0)(__va_args__)) int main() { noncopyable n; bool t; a* = new b; noncopyable& c = funcselect(type::one, *a, foo, n, t); delete a; return 0; }
not quite sure, looking for?
updated: these overloads in place, should generic.
#include <iostream> #include <utility> struct x {}; struct a: x { int f(int a, int b) { return + b; } }; struct b: x { int f(int a, int b) const { return * b; } }; template <typename d, typename b> const d& forward_cast(const b& b) { return (const d&)b; } template <typename d, typename b> d& forward_cast(b& b) { return (d&)b; } template <typename d, typename b> const d&& forward_cast(const b&& b) { return (const d&&)b; } template <typename d, typename b> d&& forward_cast(b&& b) { return (d&&)b; } // see no need use trailing return type syntax template <typename t, typename tt, typename r, typename... args, typename... argss> inline r activate(r (t::*pfn)(args...), tt&& obj, argss&&... args) { return (forward_cast<t>(std::forward<tt>(obj)).*pfn)(std::forward<argss>(args)...); } template <typename t, typename tt, typename r, typename... args, typename... argss> inline r activate(r (t::*pfn)(args...) const, tt&& obj, argss&&... args) { return (forward_cast<t>(std::forward<tt>(obj)).*pfn)(std::forward<argss>(args)...); } int main() { a; b b; x* p = &a; std::cout << activate(&a::f, *p, 1, 2) << '\n'; p = &b; std::cout << activate(&b::f, *p, 1, 2) << '\n'; }
Comments
Post a Comment