c++ - Delegate to another object's operator-> -
writing @ alias template deduce return type of type t's operator->, far have this
template <typename t> using arrow = decltype(std::declval<t&>().operator->()); which works class types, doesn't work pointers. similar problem exists trying call ->
template <typename t> struct d { t t; arrow<t> f() { // not valid call .operator->() on pointers return t.operator->(); } }; how can make function correct return type declared, , delegate correctly both class types , pointers?
for pointer, type of operator->() own type, , resulting object has same value. level of indirection, helper struct can specialized pointer types
template <typename t> struct arrowhelper { using type = decltype(std::declval<t&>().operator->()); type operator()(t& t) const { return t.operator->(); } }; template <typename t> struct arrowhelper<t*> { using type = t*; constexpr type operator()(t* t) const noexcept { return t; } }; to simplify usage, alias template , function can defined easily
template <typename t> using arrow = typename arrowhelper<t>::type; template <typename t> arrow<t> apply_arrow(t& t) { return arrowhelper<t>{}(t); } the delegating member function becomes
template <typename t> struct d { t t; arrow<t> f() { return apply_arrow(t); } };
Comments
Post a Comment