久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

std::reference_wrapper 和簡單指針的區別?

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
本文介紹了std::reference_wrapper 和簡單指針的區別?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

為什么需要 std::reference_wrapper?應該在哪里使用?它與簡單的指針有何不同?與簡單的指針相比,它的性能如何?

Why is there a need to have std::reference_wrapper? Where should it be used? How is it different from a simple pointer? How its performance compares to a simple pointer?

推薦答案

std::reference_wrapper 與模板結合使用非常有用.它通過存儲指向對象的指針來包裝對象,允許重新分配和復制,同時模仿其通常的語義.它還指示某些庫模板存儲引用而不是對象.

std::reference_wrapper is useful in combination with templates. It wraps an object by storing a pointer to it, allowing for reassignment and copy while mimicking its usual semantics. It also instructs certain library templates to store references instead of objects.

考慮 STL 中復制函子的算法:您可以通過簡單地傳遞引用函子而不是函子本身的引用包裝器來避免這種復制:

Consider the algorithms in the STL which copy functors: You can avoid that copy by simply passing a reference wrapper referring to the functor instead of the functor itself:

unsigned arr[10];
std::mt19937 myEngine;
std::generate_n( arr, 10, std::ref(myEngine) ); // Modifies myEngine's state

這是有效的,因為......

This works because…

  • reference_wrappers 重載operator() 所以它們可以像它們引用的函數對象一樣被調用:

  • reference_wrappers overload operator() so they can be called just like the function objects they refer to:

std::ref(myEngine)() // Valid expression, modifies myEngines state

  • ...(un) 像普通引用一樣,復制(和賦值)reference_wrappers 只是賦值指針.

    int i, j;
    auto r = std::ref(i); // r refers to i
    r = std::ref(j); // Okay; r refers to j
    r = std::cref(j); // Error: Cannot bind reference_wrapper<int> to <const int>
    

  • 復制一個引用包裝器實際上等同于復制一個指針,它盡可能便宜.使用它固有的所有函數調用(例如對 operator() 的調用)應該只是內聯,因為它們是單行的.

    Copying a reference wrapper is practically equivalent to copying a pointer, which is as cheap as it gets. All the function calls inherent in using it (e.g. the ones to operator()) should be just inlined as they are one-liners.

    reference_wrappers 是通過 std::ref 創建的code> 和 std::cref:

    reference_wrappers are created via std::ref and std::cref:

    int i;
    auto r = std::ref(i); // r is of type std::reference_wrapper<int>
    auto r2 = std::cref(i); // r is of type std::reference_wrapper<const int>
    

    模板參數指定所引用對象的類型和cv-qualification;r2 引用一個 const int 并且只會產生對 const int 的引用.調用包含 const 函子的引用包裝器只會調用 const 成員函數 operator()s.

    The template argument specifies the type and cv-qualification of the object referred to; r2 refers to a const int and will only yield a reference to const int. Calls to reference wrappers with const functors in them will only call const member function operator()s.

    右值初始值設定項是不允許的,因為允許它們弊大于利.由于無論如何都會移動右值(并且使用 保證復制省略即使部分避免了這種情況),我們也不會改進語義;不過,我們可以引入懸空指針,因為引用包裝器不會延長指針的生命周期.

    Rvalue initializers are disallowed, as permitting them would do more harm than good. Since rvalues would be moved anyway (and with guaranteed copy elision even that's avoided partly), we don't improve the semantics; we can introduce dangling pointers though, as a reference wrapper does not extend the pointee's lifetime.

    如前所述,可以通過將相應的參數傳遞給 reference_wrapper 來指示 make_tuple 在結果 tuple 中存儲一個引用:

    As mentioned before, one can instruct make_tuple to store a reference in the resulting tuple by passing the corresponding argument through a reference_wrapper:

    int i;
    auto t1 = std::make_tuple(i); // Copies i. Type of t1 is tuple<int>
    auto t2 = std::make_tuple(std::ref(i)); // Saves a reference to i.
                                            // Type of t2 is tuple<int&>
    

    請注意,這與 forward_as_tuple 略有不同:這里不允許將右值作為參數.

    Note that this slightly differs from forward_as_tuple: Here, rvalues as arguments are not allowed.

    std::bind 顯示相同的行為:它如果它是一個 reference_wrapper,則不會復制參數但存儲一個引用.如果該參數(或函子!)不需要復制但在使用 bind-函子時保持在范圍內,則很有用.

    std::bind shows the same behavior: It won't copy the argument but store a reference if it is a reference_wrapper. Useful if that argument (or the functor!) need not be copied but stays in scope while the bind-functor is used.

    • 沒有額外的語法間接層.指針必須被取消引用以獲得指向它們所引用對象的左值;reference_wrappers 有一個隱式的轉換運算符并且可以被調用就像它們包裹的物體一樣.

    • There is no additional level of syntactical indirection. Pointers have to be dereferenced to obtain an lvalue to the object they refer to; reference_wrappers have an implicit conversion operator and can be called like the object they wrap.

    int i;
    int& ref = std::ref(i); // Okay
    

  • reference_wrapper 與指針不同,它沒有空狀態.它們必須使用 引用或另一個 reference_wrapper.

  • reference_wrappers, unlike pointers, don't have a null state. They have to be initialized with either a reference or another reference_wrapper.

    std::reference_wrapper<int> r; // Invalid
    

  • 一個相似之處是淺拷貝語義:指針和 reference_wrapper 可以重新分配.

    這篇關于std::reference_wrapper 和簡單指針的區別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
  • 相關文檔推薦

    Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
    How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
    Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
    Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
    Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
    Is pointer comparison undefined or unspecified behavior in C++?(C++ 中的指針比較是未定義或未指定的行為嗎?)
    主站蜘蛛池模板: 国产午夜视频在线观看 | 国产精品无遮挡 | 日韩一级免费视频 | 麻豆做爰免费观看 | 91成人精品| 日韩有码在线视频 | 超碰人人人 | 高潮一区二区三区乱码 | 国产精品美女久久久久av爽 | 青草视频在线观看免费 | 亚洲色网址| 黄色片视频免费 | 日韩综合精品 | 两性免费视频 | 青青草视频网站 | 午夜亚洲精品 | 国产精品久久久久永久免费看 | 黄色福利视频 | 黄在线观看| 天天色天天干天天 | 国产精品黄 | 第一福利丝瓜av导航 | 91日韩在线 | 人人爱av| 国产网站在线 | 天天草天天干 | 亚洲欧美在线观看 | 欧美精品一级片 | 青青青草视频在线观看 | 91看片网站 | www.天天操 | 免费黄色一级视频 | 色综合久久88 | 国产乱码一区二区三区 | 日韩视频一区二区 | 狠狠的日| 国模一区二区三区 | 日韩免费成人 | 91av视频在线观看 | 久久草视频 | 秋霞啪啪片 |