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

  • <tfoot id='iqO5y'></tfoot>

    <small id='iqO5y'></small><noframes id='iqO5y'>

  • <legend id='iqO5y'><style id='iqO5y'><dir id='iqO5y'><q id='iqO5y'></q></dir></style></legend>

      <i id='iqO5y'><tr id='iqO5y'><dt id='iqO5y'><q id='iqO5y'><span id='iqO5y'><b id='iqO5y'><form id='iqO5y'><ins id='iqO5y'></ins><ul id='iqO5y'></ul><sub id='iqO5y'></sub></form><legend id='iqO5y'></legend><bdo id='iqO5y'><pre id='iqO5y'><center id='iqO5y'></center></pre></bdo></b><th id='iqO5y'></th></span></q></dt></tr></i><div class="8y3oagm" id='iqO5y'><tfoot id='iqO5y'></tfoot><dl id='iqO5y'><fieldset id='iqO5y'></fieldset></dl></div>
        <bdo id='iqO5y'></bdo><ul id='iqO5y'></ul>
      1. 為模板類重載運(yùn)算符時(shí)的隱式轉(zhuǎn)換

        Implicit conversion when overloading operators for template classes(為模板類重載運(yùn)算符時(shí)的隱式轉(zhuǎn)換)
          <bdo id='e6dRx'></bdo><ul id='e6dRx'></ul>

            <legend id='e6dRx'><style id='e6dRx'><dir id='e6dRx'><q id='e6dRx'></q></dir></style></legend>

              • <i id='e6dRx'><tr id='e6dRx'><dt id='e6dRx'><q id='e6dRx'><span id='e6dRx'><b id='e6dRx'><form id='e6dRx'><ins id='e6dRx'></ins><ul id='e6dRx'></ul><sub id='e6dRx'></sub></form><legend id='e6dRx'></legend><bdo id='e6dRx'><pre id='e6dRx'><center id='e6dRx'></center></pre></bdo></b><th id='e6dRx'></th></span></q></dt></tr></i><div class="t8rfafo" id='e6dRx'><tfoot id='e6dRx'></tfoot><dl id='e6dRx'><fieldset id='e6dRx'></fieldset></dl></div>

                  <tfoot id='e6dRx'></tfoot>

                  <small id='e6dRx'></small><noframes id='e6dRx'>

                    <tbody id='e6dRx'></tbody>
                  本文介紹了為模板類重載運(yùn)算符時(shí)的隱式轉(zhuǎn)換的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  限時(shí)送ChatGPT賬號(hào)..

                  我想知道為什么隱式類型轉(zhuǎn)換不適用于類模板上的外部運(yùn)算符重載.這是工作的非模板版本:

                  I would like to know why implicit type conversion doesn't work with outside operator overloading on class templates. Here is the working, non-templated version:

                  class foo
                  {
                  public:
                  
                      foo() = default;
                  
                      foo(int that)
                      {}
                  
                      foo& operator +=(foo rhs)
                      {
                          return *this;
                      }
                  };
                  
                  foo operator +(foo lhs, foo rhs)
                  {
                      lhs += rhs;
                      return lhs;
                  }
                  

                  正如預(yù)期的那樣,以下幾行編譯正確:

                  As expected, the following lines compile correctly:

                  foo f, g;
                  f = f + g; // OK
                  f += 5; // OK
                  f = f + 5; // OK
                  f = 5 + f; // OK
                  

                  另一方面,當(dāng)類 foo 被聲明為一個(gè)簡(jiǎn)單的模板時(shí):

                  On the other hand, when class foo is declared as a simple template like this:

                  template< typename T >
                  class foo
                  {
                  public:
                  
                      foo() = default;
                  
                      foo(int that)
                      {}
                  
                      foo& operator +=(foo rhs)
                      {
                          return *this;
                      }
                  };
                  
                  template< typename T >
                  foo< T > operator +(foo< T > lhs, foo< T > rhs)
                  {
                      lhs += rhs;
                      return lhs;
                  }
                  

                  以下幾行編譯出錯(cuò):

                  foo< int > f, g;
                  f = f + g; // OK
                  f += 5; // OK
                  f = f + 5; // Error (no match for operator+)
                  f = 5 + f; // Error (no match for operator+)
                  

                  我想了解為什么編譯器 (GCC 4.6.2) 無(wú)法使用類模板版本的轉(zhuǎn)換構(gòu)造函數(shù)執(zhí)行隱式類型轉(zhuǎn)換.這是預(yù)期的行為嗎?除了手動(dòng)創(chuàng)建所有必要的重載之外,是否有任何解決方法?

                  I would like to understand why the compiler (GCC 4.6.2) is unable to perform implicit type conversion using the converting constructor for the template version of the class. Is that the expected behaviour? Apart from manually creating all the necessary overloads, is there any workaround for this?

                  推薦答案

                  它不正常工作的原因是隱式類型轉(zhuǎn)換(即通過(guò)構(gòu)造函數(shù))在模板參數(shù)推導(dǎo)期間不適用.但是如果你讓外部操作符成為朋友,那么它就可以工作,因?yàn)轭愋?T 是已知的,允許編譯器調(diào)查可以轉(zhuǎn)換什么來(lái)使參數(shù)匹配.

                  The reason it does not just work is that implicit type conversions (that is, via constructors) do not apply during template argument deduction. But it works if you make the outside operator a friend since then the type T is know, allowing the compiler to investigate what can be casted to make the arguments match.

                  我根據(jù)您的示例(但刪除了 C++11 內(nèi)容)制作了一個(gè)示例,其靈感來(lái)自 Scott Meyers Effective C++(第 3 版)中的第 46 項(xiàng)(有理數(shù)類).您的問(wèn)題幾乎與該項(xiàng)目完全匹配.Scott 還指出……這種朋友的使用與類的非公共部分的訪問(wèn)無(wú)關(guān)."

                  I made an example based on yours (but removed C++11 stuff), inspired by Item 46 (a rational number class) in Scott Meyers Effective C++ (ed 3). Your question is almost an exact match to that item. Scott also notes that ... "this use of friend is not related to the access of non-public parts of the class."

                  這也將允許混合使用 foo<T>, foo 等等,只要可以添加 T 和 U 等等

                  This will also allow work with mixes of foo< T >, foo< U > etc as long as T and U can be added etc.

                  另看這篇文章:C++加法重載歧義

                  #include <iostream>
                  
                  using namespace std;
                  
                  template< class T >
                  class foo
                  {
                  private:
                     T _value;
                  public:
                     foo() : _value() {}
                  
                     template <class U>
                     foo(const foo<U>& that) : _value(that.getval()) {}
                  
                     // I'm sure this it can be done without this being public also;
                     T getval() const { return _value ; }; 
                  
                     foo(const T& that) : _value(that) {}
                  
                     friend const foo operator +(foo &lhs,const foo &rhs) 
                        {
                       foo result(lhs._value+rhs._value); 
                       return result;
                        };
                     friend const foo operator +(foo &lhs,const T &rhsval) 
                        {
                       foo result(lhs._value+rhsval); 
                       return result;
                        };
                     friend const foo operator +(const T &lhsval,foo &rhs) 
                        {
                       foo result(lhsval+rhs._value); 
                       return result;
                        };
                  
                     friend foo& operator +=(foo &lhs,const foo &rhs)
                        {
                       lhs._value+=rhs._value;
                       return lhs;
                        };   
                     friend std::ostream& operator<<(std::ostream& out, const foo& me){
                        return out <<me._value;
                     }
                  };
                  
                  int main(){
                     foo< int > f, g;
                     foo< double > dd;
                     cout <<f<<endl;
                     f = f + g;
                     cout <<f<<endl;
                     f += 3 ;
                     cout <<f<<endl;
                     f = f + 5;
                     cout <<f<<endl;
                     f = 7 + f; 
                     cout <<f<<endl;      
                     dd=dd+f;
                     cout <<dd<<endl;      
                     dd=f+dd;
                     cout <<dd<<endl;      
                     dd=dd+7.3;
                     cout <<dd<<endl;             
                  }
                  

                  這篇關(guān)于為模板類重載運(yùn)算符時(shí)的隱式轉(zhuǎn)換的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Why do two functions have the same address?(為什么兩個(gè)函數(shù)的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復(fù)制構(gòu)造的?)
                  mixing templates with polymorphism(混合模板與多態(tài)性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時(shí)候應(yīng)該使用關(guān)鍵字“typename?使用模板時(shí))
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標(biāo)準(zhǔn)庫(kù))
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數(shù)模板,而 clang 不能)

                  <small id='sGITI'></small><noframes id='sGITI'>

                  <i id='sGITI'><tr id='sGITI'><dt id='sGITI'><q id='sGITI'><span id='sGITI'><b id='sGITI'><form id='sGITI'><ins id='sGITI'></ins><ul id='sGITI'></ul><sub id='sGITI'></sub></form><legend id='sGITI'></legend><bdo id='sGITI'><pre id='sGITI'><center id='sGITI'></center></pre></bdo></b><th id='sGITI'></th></span></q></dt></tr></i><div class="m7r3qpn" id='sGITI'><tfoot id='sGITI'></tfoot><dl id='sGITI'><fieldset id='sGITI'></fieldset></dl></div>

                      <bdo id='sGITI'></bdo><ul id='sGITI'></ul>
                        • <legend id='sGITI'><style id='sGITI'><dir id='sGITI'><q id='sGITI'></q></dir></style></legend>

                            <tfoot id='sGITI'></tfoot>
                              <tbody id='sGITI'></tbody>
                            主站蜘蛛池模板: 包装机传感器-搅拌站传感器-山东称重传感器厂家-济南泰钦电气 | 激光内雕_led玻璃_发光玻璃_内雕玻璃_导光玻璃-石家庄明晨三维科技有限公司 激光内雕-内雕玻璃-发光玻璃 | 24位ADC|8位MCU-芯易德科技有限公司 | 安规电容|薄膜电容|陶瓷电容|智旭JEC安规电容厂家 | UV-1800紫外光度计-紫外可见光度计厂家-翱艺仪器(上海)有限公司 | 400电话_400电话申请_866元/年_【400电话官方业务办理】-俏号网 3dmax渲染-效果图渲染-影视动画渲染-北京快渲科技有限公司 | 智能终端_RTU_dcm_北斗星空自动化科技 | 上海心叶港澳台联考一对一培训_上海心叶港澳台联考,港澳台联考一对一升学指导 | 刚性-柔性防水套管-橡胶伸缩接头-波纹管补偿器-启腾供水材料有限公司 | 武汉印刷厂-不干胶标签印刷厂-武汉不干胶印刷-武汉标签印刷厂-武汉标签制作 - 善进特种标签印刷厂 | 中央空调温控器_风机盘管温控器_智能_液晶_三速开关面板-中央空调温控器厂家 | 高清视频编码器,4K音视频编解码器,直播编码器,流媒体服务器,深圳海威视讯技术有限公司 | 耐腐蚀泵,耐腐蚀真空泵,玻璃钢真空泵-淄博华舜耐腐蚀真空泵有限公司 | AGV叉车|无人叉车|AGV智能叉车|AGV搬运车-江西丹巴赫机器人股份有限公司 | 深圳市八百通智能技术有限公司官方网站 | 油漆辅料厂家_阴阳脚线_艺术漆厂家_内外墙涂料施工_乳胶漆专用防霉腻子粉_轻质粉刷石膏-魔法涂涂 | CXB船用变压器-JCZ系列制动器-HH101船用铜质开关-上海永上船舶电器厂 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | 纯化水设备-EDI-制药-实验室-二级反渗透-高纯水|超纯水设备 | 杭州代理记账多少钱-注册公司代办-公司注销流程及费用-杭州福道财务管理咨询有限公司 | 不锈钢水箱厂家,不锈钢保温水箱-山东桑特供水设备 | 协议书_协议合同格式模板范本大全| 超声波乳化机-超声波分散机|仪-超声波萃取仪-超声波均质机-精浩机械|首页 | 房车价格_依维柯/大通/东风御风/福特全顺/江铃图片_云梯搬家车厂家-程力专用汽车股份有限公司 | ptc_浴霸_大巴_干衣机_呼吸机_毛巾架_电动车加热器-上海帕克 | 深圳标识制作公司-标识标牌厂家-深圳广告标识制作-玟璟广告-深圳市玟璟广告有限公司 | 开云(中国)Kaiyun·官方网站 - 登录入口| Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 柔软云母板-硬质-水位计云母片组件-首页-武汉长丰云母绝缘材料有限公司 | 自动钻孔机-全自动数控钻孔机生产厂家-多米(广东)智能装备有限公司 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | 真空泵厂家_真空泵机组_水环泵_旋片泵_罗茨泵_耐腐蚀防爆_中德制泵 | 立式_复合式_壁挂式智能化电伴热洗眼器-上海达傲洗眼器生产厂家 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 卫生型双针压力表-高温防腐差压表-安徽康泰电气有限公司 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 立式_复合式_壁挂式智能化电伴热洗眼器-上海达傲洗眼器生产厂家 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 婚博会2024时间表_婚博会门票领取_婚博会地址-婚博会官网 | 深圳湾1号房价_深圳湾1号二手房源 | 政府园区专业委托招商平台_助力企业选址项目快速落地_东方龙商务集团 | 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 冲锋衣滑雪服厂家-冲锋衣定制工厂-滑雪服加工厂-广东睿牛户外(S-GERT) |