pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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++ 中的指針比較是未定義或未指定的行為嗎?)
    主站蜘蛛池模板: 除尘布袋_液体过滤袋_针刺毡滤料-杭州辉龙过滤技术有限公司 | 焊接减速机箱体,减速机箱体加工-淄博博山泽坤机械厂 | 上海公司注册-代理记账-招投标审计-上海昆仑扇财税咨询有限公司 上海冠顶工业设备有限公司-隧道炉,烘箱,UV固化机,涂装设备,高温炉,工业机器人生产厂家 | 衡阳耐适防护科技有限公司——威仕盾焊接防护用品官网/焊工手套/焊接防护服/皮革防护手套 | 送料机_高速冲床送料机_NC伺服滚轮送料机厂家-东莞市久谐自动化设备有限公司 | 十字轴_十字轴万向节_十字轴总成-南京万传机械有限公司 | 自动钻孔机-全自动数控钻孔机生产厂家-多米(广东)智能装备有限公司 | 杭州网络公司_百度SEO优化-外贸网络推广_抖音小程序开发-杭州乐软科技有限公司 | IHDW_TOSOKU_NEMICON_EHDW系列电子手轮,HC1系列电子手轮-上海莆林电子设备有限公司 | 创绿家招商加盟网-除甲醛加盟-甲醛治理加盟-室内除甲醛加盟-创绿家招商官网 | 螺杆泵_中成泵业| 步进驱动器「一体化」步进电机品牌厂家-一体式步进驱动 | 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | 无味渗透剂,泡沫抑尘剂,烷基糖苷-威海威能化工有限公司 | 环比机械| 江苏大隆凯科技有限公司| 盘式曝气器-微孔曝气器-管式曝气器-曝气盘-斜管填料 | 郑州市前程水处理有限公司 | 咖啡加盟,咖啡店加盟连锁品牌-卡小逗| 萃取箱-萃取槽-PVC萃取箱厂家-混合澄清槽- 杭州南方化工设备 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | EDLC超级法拉电容器_LIC锂离子超级电容_超级电容模组_软包单体电容电池_轴向薄膜电力电容器_深圳佳名兴电容有限公司_JMX专注中高端品牌电容生产厂家 | PCB设计,PCB抄板,电路板打样,PCBA加工-深圳市宏力捷电子有限公司 | 上海租车公司_上海包车_奔驰租赁_上海商务租车_上海谐焕租车 | 南京雕塑制作厂家-不锈钢雕塑制作-玻璃钢雕塑制作-先登雕塑厂 | 引领中高档酒店加盟_含舍·美素酒店品牌官网 | 气胀轴|气涨轴|安全夹头|安全卡盘|伺服纠偏系统厂家-天机传动 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 山东氧化铁红,山东铁红-淄博科瑞化工有限公司 | 液压油缸-液压站生产厂家-洛阳泰诺液压科技有限公司 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 手术室净化装修-手术室净化工程公司-华锐手术室净化厂家 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | 污水/卧式/潜水/钻井/矿用/大型/小型/泥浆泵,价格,参数,型号,厂家 - 安平县鼎千泵业制造厂 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 知企服务-企业综合服务(ZiKeys.com)-品优低价、种类齐全、过程管理透明、速度快捷高效、放心服务,知企专家! | YT保温材料_YT无机保温砂浆_外墙保温材料_南阳银通节能建材高新技术开发有限公司 | 立式_复合式_壁挂式智能化电伴热洗眼器-上海达傲洗眼器生产厂家 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 水冷散热器_水冷电子散热器_大功率散热器_水冷板散热器厂家-河源市恒光辉散热器有限公司 | 气动|电动调节阀|球阀|蝶阀-自力式调节阀-上海渠工阀门管道工程有限公司 | 贵州成人高考网_贵州成考网| 旗帜网络笔记-免费领取《旗帜网络笔记》电子书 |