Pointer-like classes and the ->* operator

血红的双手。 提交于 2019-12-04 23:33:42
n. 'pronouns' m.

You can overload ->* with a free function. It doesn't have to be a member.

template <typename P,
          typename T,
          typename M>
M& operator->* (P smartptr, M T::*ptrmem)
{
    return (*smartptr).*ptrmem;
}

Now everything that has unary operator* defined (iterators, smart pointers, whatever) can also use ->*. You may want to do it in a bit more controlled fashion, i.e. define it for known iterators, known smart pointers etc. separately.

This will not work for member functions for obvious reasons. One would need to specialize/overload for this case and return a bound std::function instead:

template <typename P,
          typename T,
          typename M,
          typename ... Arg>
std::function<M(Arg&&...)> 
operator->* (P smartptr, M (T::*ptrmem)(Arg... args))
{
    return [smartptr,ptrmem](Arg&&... args) -> M 
      { return ((*smartptr).*ptrmem)(std::forward<Arg>(args)...); };
}

Is that what you want to do ((&*ite)->*ptr) = 42; ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!