【发布时间】:2012-08-08 12:02:07
【问题描述】:
c++ 常见问题 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 实例化的吗?
'当你调用 operator+ 或 operator
事实上,为了让编译器将上面的内容识别为函数模板,程序员必须像下面这样显式地这样做:
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);
谁能解释一下?我觉得这很烦人,不知道为什么编译器不只是通过将 T 替换为 'int' 来实例化 Foo 类的实例,然后就结束了。
谢谢。
【问题讨论】:
标签: c++