C++ FAQ 35.16爲什麼朋友成員函數不能自動識別爲函數模板?
http://www.parashift.com/c++-faq-lite/template-friends.html
#include <iostream>
template<typename T>
class Foo {
public:
Foo(T const& value = T());
friend Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>& rhs);
friend std::ostream& operator<< (std::ostream& o, const Foo<T>& x);
private:
T value_;
};
的作者日期聲稱:
「當編譯器看到好友線一路類定義適當的障礙發生。那時它還不知道朋友函數本身是模板(爲什麼它不是默認的類模板成員函數是函數模板?);它假定它們是非模板是這樣的:」
Foo<int> operator+ (const Foo<int>& lhs, const Foo<int>& rhs)
{ ... }
std::ostream& operator<< (std::ostream& o, const Foo<int>& x)
{ ... }
爲什麼上述非模板?不是通過int實例化的這些模板嗎?
「當您撥打運營商+或操作員< <功能,這種假設使編譯器生成的非模板函數的調用,而是因爲你從來沒有真正定義鏈接器會給你一個‘不確定的外部’錯誤那些非模板功能。 「
事實上,使編譯器識別爲以上函數模板,程序員必須做到這一點明確如下圖所示:
template<typename T> class Foo; // pre-declare the template class itself
template<typename T> Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>& rhs);
template<typename T> std::ostream& operator<< (std::ostream& o, const Foo<T>& x);
誰能解釋一下嗎?我覺得這很令人煩惱,不知道爲什麼編譯器不會通過用'int'替換T來實例化Class Foo的實例,並將其稱爲一天。
謝謝。
我明白一些,但不是全部。與'friend Foo operator +(const Foo &lhs,const Foo & rhs);'一起,編譯器會看到什麼? operator +將是Foo的朋友函數,在返回類型,函數參數等方面沒有更多,直到前向找到operator +的聲明? –
user1559625
2012-08-08 13:47:12
@ user1559625對於任何特定的'T',編譯器都會看到'Foo operator +(const Foo &lhs,const Foo & rhs);'如果定義它將成爲朋友。已經聲明瞭一個合適的模板,然後它將被實例化。 –
ecatmur
2012-08-08 13:58:41
更正;語法不同於指定用於實例化的模板(額外的尖括號)。 – ecatmur 2012-08-08 14:17:51