c – 关于shared_ptr和指向成员运算符` – > *`和`std :: bind`的指针

最近我发现shared_ptr没有指向成员运算符的指针 – > *.我创建了简单的例子:
template <typename Pointer,typename Function,typename... Args>
auto invoke1(Pointer p,Function f,Args... args) -> decltype((p->*f)(args...))
{
  return (p->*f)(args...);
}
struct A { 
    void g() { std::cout << "A::g()\n"; } 
};
int main() {
  A a;
  invoke1(&a,&A::g); // works!!
  std::shared_ptr<A> sa = std::make_shared<A>();
  invoke1(sa,&A::g); // compile error!!
}

Q1:为什么会这样?为什么shared_ptr没有这个操作符?

我为shared_ptr添加了这样的操作符,并且该示例开始工作:

template <typename T,typename Result>
auto operator ->* (std::shared_ptr<T> pointer,Result (T::*function)()) ->decltype(std::bind(function,pointer))
{
    return std::bind(function,pointer);
}
template <typename T,typename Result,typename Arg1>
auto operator ->* (std::shared_ptr<T> pointer,Result (T::*function)(Arg1 arg1)) ->decltype(std::bind(function,pointer,std::placeholders::_1))
{
    return std::bind(function,std::placeholders::_1);
}

Q2:这个操作符是否正确实施?有没有任何“黄金”规则如何实现这样的运算符,可能是我重新发明了轮子或进入完全错误的方向,你觉得怎么样?有没有办法让一个单一的功能实现这个操作符,而不是像在std中的占位符一样多的功能

之后我得出结论,std :: bind可以在我的invoke方法中使用.

template <typename Pointer,typename... Args>
auto invoke2(Pointer p,Args... args) 
                     -> decltype(std::bind(f,p,args...)())
{
   return std::bind(f,args...)();
}

以这种方式,我的示例也可以无需添加运算符 – > *到shared_ptr.

Q3:所以,std :: bind现在被认为是替换为operator-> *?

解决方法

一个坚果shell中:是std :: bind是成员函数指针的替代.

为什么?因为成员函数指针是可怕的,他们唯一的目的是实现委托,这就是为什么std :: bind和std :: function do

有关如何实现成员函数指针的参考,请参阅我以前的答案here.简单来说,成员函数指针被标准化,因为它们不允许在转换之后进行调用;这使得他们对成员函数指针中的90%的人的行为是非常有意义的:代理.

因此,std :: function用于表示抽象的“可调用”类型,std :: bind用于将其绑定到成员函数指针.你绝对不要混淆成员函数指针,而是使用std :: bind和std ::函数.

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...