两种方法来声明模板朋友类? [英] Two ways to declare template friend classes?
本文介绍了两种方法来声明模板朋友类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
template<typename T> class FooBar {};
template<typename T> class Bar {
friend class FooBar<T>;
};
template<typename T> class Bar2 {
template friend class FooBar<T>;
};
class Bar和Bar2有什么区别?
What is the difference between class Bar and Bar2?
推荐答案
根据我的编译器,第二个有一个无效的语法。如果您将它们更改为:
The second one you have is invalid syntax, according to my compiler. If you change them to:
template<typename T> class FooBar {};
template<typename T> class Bar {
friend class FooBar<T>;
};
template<typename T> class Bar2 {
template<typename T2> friend class FooBar;
};
然后就会编译。区别在于 Bar
,只有 FooBar
如果你有 Bar
FooBar< int>
是朋友,不是 FooBar< char>
或任何其他类型,但 int
。在 Bar2< T>
中,任何类型的 FooBar
都是朋友。
Then it will compile. The difference is that in Bar<T>
, only FooBar<T>
is a friend; if you have Bar<int>
, only FooBar<int>
is a friend, not FooBar<char>
or any other type but int
. In Bar2<T>
, any type of FooBar
is a friend.
这篇关于两种方法来声明模板朋友类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文