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

  • <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="qwawimqqmiuu" id='iqO5y'><tfoot id='iqO5y'></tfoot><dl id='iqO5y'><fieldset id='iqO5y'></fieldset></dl></div>
        <bdo id='iqO5y'></bdo><ul id='iqO5y'></ul>
      1. 為模板類重載運算符時的隱式轉換

        Implicit conversion when overloading operators for template classes(為模板類重載運算符時的隱式轉換)
          <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="qwawimqqmiuu" 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>
                  本文介紹了為模板類重載運算符時的隱式轉換的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

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

                  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;
                  }
                  

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

                  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
                  

                  另一方面,當類 foo 被聲明為一個簡單的模板時:

                  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;
                  }
                  

                  以下幾行編譯出錯:

                  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) 無法使用類模板版本的轉換構造函數執(zhí)行隱式類型轉換.這是預期的行為嗎?除了手動創(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?

                  推薦答案

                  它不正常工作的原因是隱式類型轉換(即通過構造函數)在模板參數推導期間不適用.但是如果你讓外部操作符成為朋友,那么它就可以工作,因為類型 T 是已知的,允許編譯器調查可以轉換什么來使參數匹配.

                  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.

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

                  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;             
                  }
                  

                  這篇關于為模板類重載運算符時的隱式轉換的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  Why do two functions have the same address?(為什么兩個函數的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                  mixing templates with polymorphism(混合模板與多態(tài)性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 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="qwawimqqmiuu" 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>
                            主站蜘蛛池模板: 精品久久久久久久人人人人传媒 | 香蕉视频在线播放 | 国产一区二区影院 | 国产美女在线观看 | 欧美日韩一区二区在线播放 | 颜色网站在线观看 | 久久久精品视 | 欧洲精品视频一区 | 欧美久久精品 | 欧美一区二区三区精品免费 | 日韩精品视频在线播放 | 精品欧美一区二区中文字幕视频 | 久久久久成人精品 | 日韩视频精品 | 亚洲成av人影片在线观看 | 亚洲一区二区精品视频在线观看 | 亚洲欧美一区二区三区国产精品 | 欧美精品乱码久久久久久按摩 | 久久九九99 | 久久乐国产精品 | 国产九九精品视频 | 91天堂 | 在线视频一区二区 | 91久久精品国产 | 大香在线伊779 | 亚洲午夜电影 | 男人天堂网址 | 亚洲免费视频一区 | 亚洲视频三区 | 国产h视频 | 国产精品 亚洲一区 | av一级久久| 国产精品波多野结衣 | 国产日韩欧美精品一区二区 | 97精品超碰一区二区三区 | 超碰人人插 | 国产激情片在线观看 | 亚洲一区亚洲二区 | 一级毛片中国 | 久久久久久999 | 日韩午夜|