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

C++ 模板友元運算符重載

C++ template friend operator overloading(C++ 模板友元運算符重載)
本文介紹了C++ 模板友元運算符重載的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我的代碼有什么問題?

template<int E, int F>
class Float
{
 friend Float<E, F> operator+ (const Float<E, F> &lhs, const Float<E, F> &rhs);
};

G++ 只是不斷警告:

G++ just keeps warning:

float.h:7: 警告:友元聲明‘Floatoperator+(const Float&, const Float&)' 聲明了一個非模板函數

float.h:7: 警告:(如果這不是您想要的,請確保已經聲明了函數模板,并在此處的函數名稱后添加 <>)-Wno-non-template-friend 禁用此警告

我試圖添加<>在警告說明中提到的函數名稱之后,但是 g++ 給了我一個錯誤.

I tried to add <> after the function name here as mentioned in the warning note, but g++ gives me an error.

我用clang++編譯了代碼,很好,完全沒有警告.

I compiled the code with clang++, it was fine, no warning at all.

推薦答案

這只是關于語言的一個棘手方面的警告.當你聲明一個 friend 函數時,它不是聲明所在類的成員.為了方便你可以在那里定義它,但它實際上屬于命名空間.

It's just a warning about a tricky aspect of the language. When you declare a friend function, it is not a member of the class the declaration is in. You can define it there for convenience, but it actually belongs to the namespace.

在類模板中聲明一個不是模板的友元函數,仍然在命名空間中聲明了一個非模板函數.它既不是類的成員,也不是模板.但是,它是由類模板生成的.

Declaring a friend function which is not a template, inside a class template, still declares a non-template function in the namespace. It is neither a member of the class, nor itself a template. However, it is generated by the class template.

從模板生成非模板函數有點模糊.例如,您不能在 class 塊之外為該函數添加聲明.因此,您還必須在 class 塊中定義它,這是有道理的,因為類模板會生成它.

Generating non-template functions from a template is a bit hazy. For example, you cannot add a declaration for that function outside the class block. Therefore you must define it inside the class block as well, which makes sense because the class template will generate it.

關于朋友的另一個棘手的事情是class Float {} 中的聲明沒有在命名空間中聲明函數.您只能通過依賴于參數的含義重載解析來找到它,即指定參數具有 Float 類型(或引用或指針).這對于 operator+ 來說不是問題,因為無論如何它都可能被重載,并且除了用戶定義的類型之外永遠不會調用它.

Another tricky thing about friends is that the declaration inside class Float {} does not declare the function in the namespace. You can only find it through argument-dependent meaning overload resolution, i.e. specifying an that an argument has type Float (or a reference or pointer). This is not an issue for operator+, as it is likely to be overloaded anyway, and it will never be called except for with user-defined types.

舉一個潛在問題的例子,假設你有一個轉換構造函數Float::Float(Bignum const&).但是Bignum 沒有operator+.(對不起,人為的例子.)您想依靠 operator+(Float const&, Float const&) 來進行 Bignum 加法.現在 my_bignum + 3 將無法編譯,因為兩個操作數都不是 Float,所以它找不到 friend 函數.

For an example of a potential issue, imagine you have a conversion constructor Float::Float( Bignum const& ). But Bignum does not have operator+. (Sorry, contrived example.) You want to rely on operator+(Float const&, Float const&) for Bignum addition. Now my_bignum + 3 will not compile because neither operand is a Float so it cannot find the friend function.

也許,您無需擔心,只要所討論的函數是 operator.

Probably, you have nothing to worry about, as long as the function in question is an operator.

或者,您也可以將 friend 更改為模板.在這種情況下,它必須在 class {} 塊的外部定義,并在它之前聲明,而不需要在內部聲明和定義.

Or, you can change the friend to be a template as well. In that case, it must be defined outside the class {} block, and declared before it, instead of needing to be declared and defined inside.

template<int E, int F> // now this is a template!
Float<E, F> operator+ (const Float<E, F> &lhs, const Float<E, F> &rhs);

template<int E, int F>
class Float
{
  // deduce arguments E and F - this names operator+< E, F >.
 friend Float<E, F> operator+<> (const Float<E, F> &lhs, const Float<E, F> &rhs);
};

這篇關于C++ 模板友元運算符重載的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 日本高清视频在线播放 | 超碰成人免费 | 久久福利 | 在线色网站| 欧美在线视频一区二区 | 精品自拍视频 | 中文字幕a√| 国产小视频在线 | 中文字幕一区二区三区日韩精品 | 日韩三级在线观看 | 男女羞羞视频大全 | 久久久久国产精品一区二区 | 日韩欧美在线一区二区 | 国产精品久久久久久久久大全 | 激情 一区| 中国一级特黄毛片大片 | 一级片在线观看 | 欧美福利视频一区 | 97影院2| 国产精品亚洲成在人线 | 一本一道久久a久久精品蜜桃 | 黄网站涩免费蜜桃网站 | 国产精品一码二码三码在线 | 一区二区三区欧美 | 免费在线观看av片 | 日本不卡一区二区三区在线观看 | 一级国产精品一级国产精品片 | 亚洲bt 欧美bt 日本bt | 人人叉 | 欧美一级艳情片免费观看 | 色综合视频| 成年人视频免费在线观看 | 久久一区精品 | 精品一区二区久久久久久久网站 | 国产成人影院 | 日韩精品国产精品 | 成人一区二区在线 | 午夜精品久久久久久久久久久久久 | 精品国产欧美一区二区三区成人 | 精品久久久久久亚洲精品 | 91一区|