簡體   English   中英

關於shared_ptr和指向成員運算符` - > *`和`std :: bind`的指針

[英]About shared_ptr and pointer to member operator `->*` and `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, pointer, std::placeholders::_1);
}

Q2:這個運營商是否正確實施? 是否有任何“黃金”規則如何實施這樣的操作員,可能要么我重新發明輪子或進入完全錯誤的方向,你怎么看? 有沒有辦法讓一個函數實現這個運算符,而不是像std中的占位符一樣多的函數...

之后我得出結論, std::bind可以在我的invoke方法中使用。

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

通過這種方式,我的示例也可以無需向shared_ptr添加operator ->*

Q3:那么, std::bind現在被認為是operator->*的替代operator->*

在一個堅果shell中: 是的std :: bind是成員函數指針的替代品。

為什么? 因為成員函數指針很糟糕,它們唯一的目的是實現委托,這就是為什么std :: bind和std :: function做的

有關成員函數指針是如何實現的參考,看看我以前的答案在這里 簡單來說,成員函數指針被標准削弱了,因為它們在強制轉換后不允許調用; 這使得它們對於90%的人想要來自成員函數指針的行為毫無意義:委托。

出於這個原因,std :: function用於表示抽象的“可調用”類型,std :: bind用於將this綁定到成員函數指針。 你絕對不應該使用成員函數指針,而是使用std :: bind和std :: function。

我相信最簡單的靈魂就是用一對derefence( * )和結構引用( . )運算符替換'structure dereference'( -> )運算符:

template <typename Pointer, typename Function, typename... Args>
auto invoke1(Pointer p, Function f, Args... args) -> decltype(((*p).*f)(args...))
{
  return ((*p).*f)(args...);
}

我相信shared_ptr沒有operator ->*因為不可能為任意數量的參數實現它(C ++ 11允許為其他用例執行)。 此外,您可以輕松地為調用get()智能指針添加invoke函數的重載,因此不希望使接口復雜化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM